#include <bsl_err.h>
#include <bsl_sal.h>
#include <crypt_algid.h>
#include <crypt_eal_cipher.h>
#include <crypt_eal_provider.h>
#include <crypt_errno.h>
#include <crypt_aes.h>

#include <securec.h>
#include <setjmp.h>
#include <stdarg.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>

#include <cmocka.h>

#include "helper.h"
#include <kaep_err.h>

#define GROUP_TEST_MAX 200
#define AES_BLOCKSIZE  16
#define BLOCKSIZE      16
#define MAX_OUTPUT     5000
#define MCT_INNER_LOOP 1000

typedef struct {
    int32_t id;
    char *keyHex;
    char *ivHex;
    int32_t temp;
    int32_t padding;
    int isSetPadding;
    char *inHex;
    char *outHex;
} CRYPT_AES_Hex;

typedef struct {
    int32_t id;
    char *keyHex;
    char *ivHex;
    char *inPartAHex;
    char *inPartBHex;
    char *inPartCHex;
    char *outHex;
    int32_t enc;
} CRYPT_AES_MultiData;

static CRYPT_EAL_LibCtx *CreateLoadLibCtx()
{
    CRYPT_EAL_LibCtx *libctx = CRYPT_EAL_LibCtxNew();
    assert_non_null(libctx);
    assert_int_equal(CRYPT_EAL_ProviderSetLoadPath(libctx, "../../build"), CRYPT_SUCCESS);
    assert_int_equal(CRYPT_EAL_ProviderLoad(libctx, BSL_SAL_LIB_FMT_LIBSO, "kae_provider", NULL, NULL), CRYPT_SUCCESS);
    return libctx;
}

static void SDV_KAE_AES_PROVIDER_LOAD(void **state)
{
    (void)state;
    CRYPT_EAL_LibCtx *libctx = CreateLoadLibCtx();
    CRYPT_EAL_LibCtxFree(libctx);
}

static int32_t KAE_AES_ALG_ID[] = {
    CRYPT_CIPHER_AES128_ECB, CRYPT_CIPHER_AES128_CBC, CRYPT_CIPHER_AES128_OFB, CRYPT_CIPHER_AES128_CTR,

    CRYPT_CIPHER_AES192_ECB, CRYPT_CIPHER_AES192_CBC, CRYPT_CIPHER_AES192_OFB, CRYPT_CIPHER_AES192_CTR,

    CRYPT_CIPHER_AES256_ECB, CRYPT_CIPHER_AES256_CBC, CRYPT_CIPHER_AES256_OFB, CRYPT_CIPHER_AES256_CTR,
};

static void SDV_KAE_AES_NEW_API_TC001(void **state)
{
    int32_t *id = *state;
    CRYPT_EAL_LibCtx *libctx = CreateLoadLibCtx();
    CRYPT_EAL_CipherCtx *ctx = CRYPT_EAL_ProviderCipherNewCtx(libctx, *id, "provider=kaev1");
    assert_non_null(ctx);
    CRYPT_EAL_CipherFreeCtx(ctx);
    CRYPT_EAL_LibCtxFree(libctx);
}

static CRYPT_AES_Hex SDV_KAE_AES_INIT_API_TC001_VEC[] = {
    {
        CRYPT_CIPHER_AES128_CBC,
        "1f8e4973953f3fb0bd6b16662e9a3c17",
        "2fe2b333ceda8f98f4a99b40d2cd34a8",
        0,
        0,
        0,
        NULL,
        NULL,
    },
};

static void SDV_KAE_AES_INIT_API_TC001(void **state)
{
    CRYPT_AES_Hex *test = (CRYPT_AES_Hex *)*state;

    Hex *key = NewHex();
    Hex *iv = NewHex();

    assert_int_equal(ConvertHex(test->keyHex, key), 0);
    assert_int_equal(ConvertHex(test->ivHex, iv), 0);

    CRYPT_EAL_LibCtx *libctx = CreateLoadLibCtx();
    CRYPT_EAL_CipherCtx *ctx = CRYPT_EAL_ProviderCipherNewCtx(libctx, CRYPT_CIPHER_AES128_CBC, "provider=kaev1");
    assert_non_null(ctx);

    assert_int_equal(CRYPT_EAL_CipherInit(ctx, key->x, key->len, iv->x, iv->len, true), CRYPT_SUCCESS);
    assert_int_equal(CRYPT_EAL_CipherInit(NULL, key->x, key->len, iv->x, iv->len, true), CRYPT_NULL_INPUT);
    assert_int_equal(CRYPT_EAL_CipherInit(ctx, NULL, key->len, iv->x, iv->len, true), CRYPT_NULL_INPUT);
    assert_int_equal(CRYPT_EAL_CipherInit(ctx, key->x, 0, iv->x, iv->len, true), CRYPT_INVALID_ARG);
    assert_int_equal(CRYPT_EAL_CipherInit(ctx, key->x, key->len, NULL, iv->len, true), CRYPT_NULL_INPUT);
    assert_int_equal(CRYPT_EAL_CipherInit(ctx, key->x, key->len, iv->x, 0, true), CRYPT_MODES_IVLEN_ERROR);

    FreeHex(key);
    FreeHex(iv);

    CRYPT_EAL_CipherFreeCtx(ctx);
    CRYPT_EAL_LibCtxFree(libctx);
}

static void SDV_KAE_AES_DEINIT_API_TC001(void **state)
{
    (void)state;
    CRYPT_EAL_LibCtx *libctx = CreateLoadLibCtx();
    CRYPT_EAL_CipherCtx *ctx = CRYPT_EAL_ProviderCipherNewCtx(libctx, CRYPT_CIPHER_AES128_CBC, "provider=kaev1");
    assert_non_null(ctx);

    CRYPT_EAL_CipherDeinit(ctx);
    CRYPT_EAL_CipherDeinit(NULL);

    CRYPT_EAL_CipherFreeCtx(ctx);
    CRYPT_EAL_LibCtxFree(libctx);
}

static CRYPT_AES_Hex SDV_KAE_AES_REINIT_API_TC001_VEC[] = {
    {
        CRYPT_CIPHER_AES128_CBC,
        "1f8e4973953f3fb0bd6b16662e9a3c17",
        "2fe2b333ceda8f98f4a99b40d2cd34a8",
        0,
        0,
        0,
        NULL,
        NULL,
    },
};

static void SDV_KAE_AES_REINIT_API_TC001(void **state)
{
    CRYPT_AES_Hex *test = (CRYPT_AES_Hex *)*state;

    Hex *key = NewHex();
    Hex *iv = NewHex();

    assert_int_equal(ConvertHex(test->keyHex, key), 0);
    assert_int_equal(ConvertHex(test->ivHex, iv), 0);

    CRYPT_EAL_LibCtx *libctx = CreateLoadLibCtx();
    CRYPT_EAL_CipherCtx *ctx = CRYPT_EAL_ProviderCipherNewCtx(libctx, CRYPT_CIPHER_AES128_CBC, "provider=kaev1");
    assert_non_null(ctx);

    assert_int_equal(CRYPT_EAL_CipherReinit(ctx, iv->x, iv->len), CRYPT_EAL_ERR_STATE);
    assert_int_equal(CRYPT_EAL_CipherInit(ctx, key->x, key->len, iv->x, iv->len, true), CRYPT_SUCCESS);
    assert_int_equal(CRYPT_EAL_CipherReinit(NULL, iv->x, iv->len), CRYPT_NULL_INPUT);
    assert_int_equal(CRYPT_EAL_CipherReinit(ctx, NULL, iv->len), CRYPT_NULL_INPUT);
    assert_int_equal(CRYPT_EAL_CipherReinit(ctx, iv->x, 0), CRYPT_MODES_IVLEN_ERROR);

    FreeHex(key);
    FreeHex(iv);

    CRYPT_EAL_CipherFreeCtx(ctx);
    CRYPT_EAL_LibCtxFree(libctx);
}

static CRYPT_AES_Hex SDV_KAE_AES_UPDATE_API_TC001_VEC[] = {
    {
        CRYPT_CIPHER_AES128_CBC,
        "1f8e4973953f3fb0bd6b16662e9a3c17",
        "2fe2b333ceda8f98f4a99b40d2cd34a8",
        0,
        0,
        0,
        "45cf12964fc824ab76616ae2f4bf0822",
        NULL,
    },
};

static void SDV_KAE_AES_UPDATE_API_TC001(void **state)
{
    CRYPT_AES_Hex *hex = (CRYPT_AES_Hex *)*state;

    Hex *key = NewHex();
    Hex *iv = NewHex();
    Hex *in = NewHex();

    assert_int_equal(ConvertHex(hex->keyHex, key), 0);
    assert_int_equal(ConvertHex(hex->ivHex, iv), 0);
    assert_int_equal(ConvertHex(hex->inHex, in), 0);

    uint8_t out[BLOCKSIZE] = {0};
    uint32_t len = BLOCKSIZE;

    CRYPT_EAL_LibCtx *libctx = CreateLoadLibCtx();
    CRYPT_EAL_CipherCtx *ctx = CRYPT_EAL_ProviderCipherNewCtx(libctx, CRYPT_CIPHER_AES128_CBC, "provider=kaev1");
    assert_non_null(ctx);

    assert_int_equal(CRYPT_EAL_CipherInit(ctx, key->x, key->len, iv->x, iv->len, hex->temp), CRYPT_SUCCESS);
    assert_int_equal(CRYPT_EAL_CipherUpdate(NULL, in->x, in->len, out, &len), CRYPT_NULL_INPUT);
    len = BLOCKSIZE;
    assert_int_equal(CRYPT_EAL_CipherUpdate(ctx, NULL, in->len, out, &len), CRYPT_NULL_INPUT);
    len = BLOCKSIZE;
    assert_int_equal(CRYPT_EAL_CipherUpdate(ctx, in->x, in->len, NULL, &len), CRYPT_NULL_INPUT);
    len = BLOCKSIZE;
    assert_int_equal(CRYPT_EAL_CipherUpdate(ctx, in->x, 0, NULL, &len), CRYPT_NULL_INPUT);
    len = BLOCKSIZE;
    assert_int_equal(CRYPT_EAL_CipherUpdate(ctx, in->x, 1, out, &len), CRYPT_SUCCESS);
    assert_int_equal(CRYPT_EAL_CipherUpdate(ctx, in->x, in->len, out, NULL), CRYPT_NULL_INPUT);
    len = BLOCKSIZE;
    assert_int_equal(CRYPT_EAL_CipherUpdate(ctx, in->x, in->len, out, &len), CRYPT_SUCCESS);
    assert_int_equal(len, BLOCKSIZE);

    FreeHex(key);
    FreeHex(iv);
    FreeHex(in);
    CRYPT_EAL_CipherFreeCtx(ctx);
    CRYPT_EAL_LibCtxFree(libctx);
}

static CRYPT_AES_Hex SDV_KAE_AES_FINAL_API_TC001_VEC[] = {
    {
        CRYPT_CIPHER_AES128_CBC,
        "1f8e4973953f3fb0bd6b16662e9a3c17",
        "2fe2b333ceda8f98f4a99b40d2cd34a8",
        0,
        0,
        0,
        "45cf12964fc824ab76616ae2f4bf0822",
        NULL,
    },
};

void SDV_KAE_AES_FINAL_API_TC001(void **state)
{
    CRYPT_AES_Hex *hex = (CRYPT_AES_Hex *)*state;

    Hex *key = NewHex();
    Hex *iv = NewHex();
    Hex *in = NewHex();

    assert_int_equal(ConvertHex(hex->keyHex, key), 0);
    assert_int_equal(ConvertHex(hex->ivHex, iv), 0);
    assert_int_equal(ConvertHex(hex->inHex, in), 0);

    int32_t ret;
    uint8_t out[AES_BLOCKSIZE] = {0};
    uint32_t len = AES_BLOCKSIZE;
    uint32_t finLen = AES_BLOCKSIZE;

    CRYPT_EAL_LibCtx *libctx = CreateLoadLibCtx();
    CRYPT_EAL_CipherCtx *ctx = CRYPT_EAL_ProviderCipherNewCtx(libctx, CRYPT_CIPHER_AES128_CBC, "provider=kaev1");
    assert_non_null(ctx);

    ret = CRYPT_EAL_CipherInit(ctx, key->x, key->len, iv->x, iv->len, true);
    assert_int_equal(ret, CRYPT_SUCCESS);
    ret = CRYPT_EAL_CipherUpdate(ctx, in->x, in->len, out, &len);
    assert_int_equal(ret, CRYPT_SUCCESS);
    ret = CRYPT_EAL_CipherFinal(ctx, out, &finLen);
    assert_int_equal(ret, CRYPT_SUCCESS);
    ret = CRYPT_EAL_CipherFinal(NULL, out, &finLen);
    assert_int_equal(ret, CRYPT_NULL_INPUT);
    ret = CRYPT_EAL_CipherFinal(ctx, NULL, &finLen);
    assert_int_equal(ret, CRYPT_NULL_INPUT);
    ret = CRYPT_EAL_CipherFinal(ctx, out, NULL);
    assert_int_equal(ret, CRYPT_NULL_INPUT);

    FreeHex(key);
    FreeHex(iv);
    FreeHex(in);
    CRYPT_EAL_CipherFreeCtx(ctx);
    CRYPT_EAL_LibCtxFree(libctx);
}

static CRYPT_AES_Hex SDV_KAE_AES_CTRL_API_TC001_VEC[] = {
    {
        CRYPT_CIPHER_AES128_CBC,
        "1f8e4973953f3fb0bd6b16662e9a3c17",
        "2fe2b333ceda8f98f4a99b40d2cd34a8",
        0,
        0,
        0,
        "45cf12964fc824ab76616ae2f4bf0822",
        NULL,
    },
};

static void SDV_KAE_AES_CTRL_API_TC001(void **state)
{
    CRYPT_AES_Hex *hex = (CRYPT_AES_Hex *)*state;

    Hex *key = NewHex();
    Hex *iv = NewHex();

    assert_int_equal(ConvertHex(hex->keyHex, key), 0);
    assert_int_equal(ConvertHex(hex->ivHex, iv), 0);

    uint8_t *ivGet[AES_BLOCKSIZE] = {0};
    const uint32_t len = AES_BLOCKSIZE;
    uint32_t blockSizeGet = 0;

    CRYPT_EAL_LibCtx *libctx = CreateLoadLibCtx();
    CRYPT_EAL_CipherCtx *ctx = CRYPT_EAL_ProviderCipherNewCtx(libctx, CRYPT_CIPHER_AES128_CBC, "provider=kaev1");
    assert_non_null(ctx);

    assert_int_equal(CRYPT_EAL_CipherInit(ctx, key->x, key->len, iv->x, iv->len, true), CRYPT_SUCCESS);
    assert_int_equal(CRYPT_EAL_CipherCtrl(ctx, CRYPT_CTRL_GET_IV, ivGet, len), CRYPT_SUCCESS);

    assert_int_equal(CRYPT_EAL_CipherCtrl(ctx, CRYPT_CTRL_GET_BLOCKSIZE, (uint8_t *)&blockSizeGet, sizeof(uint32_t)),
                     CRYPT_SUCCESS);
    if (hex->id == CRYPT_CIPHER_AES128_CBC) {
        assert_int_equal(blockSizeGet, AES_BLOCKSIZE);
    } else {
        assert_int_equal(blockSizeGet, 1);
    }

    assert_int_equal(CRYPT_EAL_CipherCtrl(ctx, CRYPT_CTRL_GET_IV, NULL, len), CRYPT_NULL_INPUT);
    assert_int_equal(CRYPT_EAL_CipherCtrl(ctx, CRYPT_CTRL_GET_IV, ivGet, 0), CRYPT_MODE_ERR_INPUT_LEN);
    assert_int_equal(CRYPT_EAL_CipherCtrl(ctx, CRYPT_CTRL_GET_BLOCKSIZE, (uint8_t *)&blockSizeGet, 0),
                     CRYPT_MODE_ERR_INPUT_LEN);
    assert_int_equal(CRYPT_EAL_CipherCtrl(ctx, CRYPT_CTRL_MAX, iv->x, iv->len), CRYPT_MODES_CTRL_TYPE_ERROR);
    assert_int_equal(CRYPT_EAL_CipherCtrl(ctx, CRYPT_CTRL_GET_BLOCKSIZE, NULL, sizeof(uint32_t)),
                     CRYPT_MODE_ERR_INPUT_LEN);

    FreeHex(key);
    FreeHex(iv);

    CRYPT_EAL_CipherFreeCtx(ctx);
    CRYPT_EAL_LibCtxFree(libctx);
}

static CRYPT_AES_Hex SDV_KAE_AES_SET_PADDING_API_TC001_VEC[] = {
    {
        CRYPT_CIPHER_AES128_CBC,
        "1f8e4973953f3fb0bd6b16662e9a3c17",
        "2fe2b333ceda8f98f4a99b40d2cd34a8",
        0,
        0,
        0,
        "45cf12964fc824ab76616ae2f4bf0822",
        NULL,
    },
};

static void SDV_KAE_AES_SET_PADDING_API_TC001(void **state)
{
    CRYPT_AES_Hex *hex = (CRYPT_AES_Hex *)*state;

    Hex *key = NewHex();
    Hex *iv = NewHex();

    assert_int_equal(ConvertHex(hex->keyHex, key), 0);
    assert_int_equal(ConvertHex(hex->ivHex, iv), 0);

    CRYPT_EAL_LibCtx *libctx = CreateLoadLibCtx();
    CRYPT_EAL_CipherCtx *ctxCBC = CRYPT_EAL_ProviderCipherNewCtx(libctx, CRYPT_CIPHER_AES128_CBC, "provider=kaev1");
    assert_non_null(ctxCBC);
    CRYPT_EAL_CipherCtx *ctxCTR = CRYPT_EAL_ProviderCipherNewCtx(libctx, CRYPT_CIPHER_AES128_CTR, "provider=kaev1");
    assert_non_null(ctxCTR);

    assert_int_equal(CRYPT_EAL_CipherInit(ctxCBC, key->x, key->len, iv->x, iv->len, true), CRYPT_SUCCESS);
    assert_int_equal(CRYPT_EAL_CipherSetPadding(ctxCBC, CRYPT_PADDING_ZEROS), CRYPT_SUCCESS);
    assert_int_equal(CRYPT_EAL_CipherSetPadding(NULL, CRYPT_PADDING_ZEROS), CRYPT_NULL_INPUT);
    assert_int_equal(CRYPT_EAL_CipherSetPadding(ctxCBC, CRYPT_PADDING_MAX_COUNT), CRYPT_MODES_PADDING_NOT_SUPPORT);
    assert_int_equal(CRYPT_EAL_CipherSetPadding(ctxCTR, CRYPT_PADDING_ZEROS), CRYPT_SUCCESS);

    FreeHex(key);
    FreeHex(iv);

    CRYPT_EAL_CipherFreeCtx(ctxCBC);
    CRYPT_EAL_CipherFreeCtx(ctxCTR);
    CRYPT_EAL_LibCtxFree(libctx);
}

static CRYPT_AES_Hex SDV_KAE_AES_GET_PADDING_API_TC001_VEC[] = {
    {
        CRYPT_CIPHER_AES128_CBC,
        "1f8e4973953f3fb0bd6b16662e9a3c17",
        "2fe2b333ceda8f98f4a99b40d2cd34a8",
        0,
        0,
        0,
        "45cf12964fc824ab76616ae2f4bf0822",
        NULL,
    },
};

static void SDV_KAE_AES_GET_PADDING_API_TC001(void **state)
{
    CRYPT_AES_Hex *hex = (CRYPT_AES_Hex *)*state;

    Hex *key = NewHex();
    Hex *iv = NewHex();

    assert_int_equal(ConvertHex(hex->keyHex, key), 0);
    assert_int_equal(ConvertHex(hex->ivHex, iv), 0);

    CRYPT_EAL_LibCtx *libctx = CreateLoadLibCtx();
    CRYPT_EAL_CipherCtx *ctx = CRYPT_EAL_ProviderCipherNewCtx(libctx, CRYPT_CIPHER_AES128_CBC, "provider=kaev1");
    assert_non_null(ctx);

    assert_int_equal(CRYPT_EAL_CipherInit(ctx, key->x, key->len, iv->x, iv->len, true), CRYPT_SUCCESS);
    assert_int_equal(CRYPT_EAL_CipherSetPadding(ctx, CRYPT_PADDING_ZEROS), CRYPT_SUCCESS);
    assert_int_equal(CRYPT_EAL_CipherGetPadding(ctx), CRYPT_PADDING_ZEROS);
    assert_int_equal(CRYPT_EAL_CipherGetPadding(NULL), CRYPT_NULL_INPUT);

    FreeHex(key);
    FreeHex(iv);

    CRYPT_EAL_CipherFreeCtx(ctx);
    CRYPT_EAL_LibCtxFree(libctx);
}

static CRYPT_AES_Hex SDV_KAE_AES_ENCRYPT_FUNC_TC001_VEC[] = {
    {
        CRYPT_CIPHER_AES128_ECB,
        "edfdb257cb37cdf182c5455b0c0efebb",
        "",
        1,
        0,
        0,
        "1695fe475421cace3557daca01f445ff",
        "7888beae6e7a426332a7eaa2f808e637",
    },
    {
        CRYPT_CIPHER_AES128_ECB,
        "edfdb257cb37cdf182c5455b0c0efebb",
        "",
        0,
        0,
        0,
        "7888beae6e7a426332a7eaa2f808e637",
        "1695fe475421cace3557daca01f445ff",
    },
    {
        CRYPT_CIPHER_AES128_ECB,
        "4c35be028e1475278346eae531cbee5c",
        "",
        1,
        0,
        0,
        "7adcf4a494f6b09790c82c8bb97db62c5d3fa4032f06dfeceaad9ecb374b747bd1c08d07e78e351dc2eb99bfa714d23cffe31f5fb5a472e6e0252f35a20c304c4f6d0cf7d29c99444d40af3a00a92fc86c6444fcb80ce9765362ac1bdba0b10e",
        "22217953f71932ab4360d97ef495081559f1fcb09caca41fa0c65f7b1792b560eabe18f3b3b06ef80c41886f24c5d6d32d20427e83d8b5564d9ac7435a2842c1cf7c6fcc229eb7f518d3e0167d510efbaee39a0438fc800eb6acfc203c93280c",
    },
    {
        CRYPT_CIPHER_AES128_ECB,
        "4c35be028e1475278346eae531cbee5c",
        "",
        0,
        0,
        0,
        "22217953f71932ab4360d97ef495081559f1fcb09caca41fa0c65f7b1792b560eabe18f3b3b06ef80c41886f24c5d6d32d20427e83d8b5564d9ac7435a2842c1cf7c6fcc229eb7f518d3e0167d510efbaee39a0438fc800eb6acfc203c93280c",
        "7adcf4a494f6b09790c82c8bb97db62c5d3fa4032f06dfeceaad9ecb374b747bd1c08d07e78e351dc2eb99bfa714d23cffe31f5fb5a472e6e0252f35a20c304c4f6d0cf7d29c99444d40af3a00a92fc86c6444fcb80ce9765362ac1bdba0b10e",
    },
    {
        CRYPT_CIPHER_AES192_ECB,
        "61396c530cc1749a5bab6fbcf906fe672d0c4ab201af4554",
        "",
        1,
        0,
        0,
        "60bcdb9416bac08d7fd0d780353740a5",
        "24f40c4eecd9c49825000fcb4972647a",
    },
    {
        CRYPT_CIPHER_AES192_ECB,
        "61396c530cc1749a5bab6fbcf906fe672d0c4ab201af4554",
        "",
        0,
        0,
        0,
        "24f40c4eecd9c49825000fcb4972647a",
        "60bcdb9416bac08d7fd0d780353740a5",
    },
    {
        CRYPT_CIPHER_AES192_ECB,
        "b07da99fbe0b193e5336141a9819974b1a9a4102129db861",
        "",
        1,
        0,
        0,
        "27756c11f57c40169f95f25141fb1a07d9444d864e4c43b9b16f39fbc2fb9529b6e4c9fb37ba7994add9e057bce070fd0d64b8e452e58baa84005ffb6fe7f3a7710f35f87f28624268526fbb5e8ea88db272972adf2227e0ca7b09ad48915754",
        "7516e53877236e931e7fbae22ed2ca5116b72a700f6e397c2aa47c304b3f5c2a026d89831a5104ae7b73ef48de709c1b9e661ae973dea6bb38217a7650228ccc6b228b33f567121427e9f21cbd3ec6aa35ebba336f51ed8e4bf6e75c5866bc20",
    },
    {
        CRYPT_CIPHER_AES192_ECB,
        "b07da99fbe0b193e5336141a9819974b1a9a4102129db861",
        "",
        0,
        0,
        0,
        "7516e53877236e931e7fbae22ed2ca5116b72a700f6e397c2aa47c304b3f5c2a026d89831a5104ae7b73ef48de709c1b9e661ae973dea6bb38217a7650228ccc6b228b33f567121427e9f21cbd3ec6aa35ebba336f51ed8e4bf6e75c5866bc20",
        "27756c11f57c40169f95f25141fb1a07d9444d864e4c43b9b16f39fbc2fb9529b6e4c9fb37ba7994add9e057bce070fd0d64b8e452e58baa84005ffb6fe7f3a7710f35f87f28624268526fbb5e8ea88db272972adf2227e0ca7b09ad48915754",
    },
    {
        CRYPT_CIPHER_AES256_ECB,
        "cc22da787f375711c76302bef0979d8eddf842829c2b99ef3dd04e23e54cc24b",
        "",
        1,
        0,
        0,
        "ccc62c6b0a09a671d64456818db29a4d",
        "df8634ca02b13a125b786e1dce90658b",
    },
    {
        CRYPT_CIPHER_AES256_ECB,
        "cc22da787f375711c76302bef0979d8eddf842829c2b99ef3dd04e23e54cc24b",
        "",
        0,
        0,
        0,
        "df8634ca02b13a125b786e1dce90658b",
        "ccc62c6b0a09a671d64456818db29a4d",
    },
    {
        CRYPT_CIPHER_AES256_ECB,
        "1d29079cc34ab5a3bc713f416a129f9d26ada15fca458cc2731404ea857d2f79",
        "",
        1,
        0,
        0,
        "b36809198132518d815aa37f32f40dd7a952ec8bd63355700837b450b396b33c72125e23482a84a42b916021c3de78156f85c6a78906167feb64afd8b1d935d641c8ce1a89f3849588eeeb9910d40336ca385fc37a5e87bb84ab9ccbb05b3a28",
        "7ae803b14914d156439f580c02c592bf9a41b7b80c20168129a33fae2290403f01a6fabe4fea7ac770ffbc6c421f8e013b9e831674ef17eb27d446dded3baf50686b809c18b6bbd588cd3c7423b64987e9164b7e1e66987dc9319790ae27b3e8",
    },
    {
        CRYPT_CIPHER_AES256_ECB,
        "1d29079cc34ab5a3bc713f416a129f9d26ada15fca458cc2731404ea857d2f79",
        "",
        0,
        0,
        0,
        "7ae803b14914d156439f580c02c592bf9a41b7b80c20168129a33fae2290403f01a6fabe4fea7ac770ffbc6c421f8e013b9e831674ef17eb27d446dded3baf50686b809c18b6bbd588cd3c7423b64987e9164b7e1e66987dc9319790ae27b3e8",
        "b36809198132518d815aa37f32f40dd7a952ec8bd63355700837b450b396b33c72125e23482a84a42b916021c3de78156f85c6a78906167feb64afd8b1d935d641c8ce1a89f3849588eeeb9910d40336ca385fc37a5e87bb84ab9ccbb05b3a28",
    },
    {
        CRYPT_CIPHER_AES128_CBC,
        "00000000000000000000000000000000",
        "00000000000000000000000000000000",
        1,
        0,
        0,
        "f34481ec3cc627bacd5dc3fb08f273e6",
        "0336763e966d92595a567cc9ce537f5e",
    },
    {
        CRYPT_CIPHER_AES192_CBC,
        "000000000000000000000000000000000000000000000000",
        "00000000000000000000000000000000",
        1,
        0,
        0,
        "1b077a6af4b7f98229de786d7516b639",
        "275cfc0413d8ccb70513c3859b1d0f72",
    },
    {
        CRYPT_CIPHER_AES256_CBC,
        "0000000000000000000000000000000000000000000000000000000000000000",
        "00000000000000000000000000000000",
        1,
        0,
        0,
        "014730f80ac625fe84f026c60bfd547d",
        "5c9d844ed46f9885085e5d6a4f94c7d7",
    },
    {
        CRYPT_CIPHER_AES128_CBC,
        "00000000000000000000000000000000",
        "00000000000000000000000000000000",
        0,
        0,
        0,
        "0336763e966d92595a567cc9ce537f5e",
        "f34481ec3cc627bacd5dc3fb08f273e6",
    },
    {
        CRYPT_CIPHER_AES192_CBC,
        "000000000000000000000000000000000000000000000000",
        "00000000000000000000000000000000",
        0,
        0,
        0,
        "275cfc0413d8ccb70513c3859b1d0f72",
        "1b077a6af4b7f98229de786d7516b639",
    },
    {
        CRYPT_CIPHER_AES256_CBC,
        "0000000000000000000000000000000000000000000000000000000000000000",
        "00000000000000000000000000000000",
        0,
        0,
        0,
        "5c9d844ed46f9885085e5d6a4f94c7d7",
        "014730f80ac625fe84f026c60bfd547d",
    },
    {
        CRYPT_CIPHER_AES128_CTR,
        "2b7e151628aed2a6abf7158809cf4f3c",
        "f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff",
        1,
        0,
        0,
        "6bc1bee22e409f96e93d7e117393172a",
        "874d6191b620e3261bef6864990db6ce",
    },
    {
        CRYPT_CIPHER_AES192_CTR,
        "8e73b0f7da0e6452c810f32b809079e562f8ead2522c6b7b",
        "f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff",
        1,
        0,
        0,
        "6bc1bee22e409f96e93d7e117393172a",
        "1abc932417521ca24f2b0459fe7e6e0b",
    },
    {
        CRYPT_CIPHER_AES256_CTR,
        "603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4",
        "f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff",
        1,
        0,
        0,
        "6bc1bee22e409f96e93d7e117393172a",
        "601ec313775789a5b7a7f504bbf3d228",
    },
    {
        CRYPT_CIPHER_AES128_CTR,
        "2b7e151628aed2a6abf7158809cf4f3c",
        "f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff",
        0,
        0,
        0,
        "874d6191b620e3261bef6864990db6ce",
        "6bc1bee22e409f96e93d7e117393172a",
    },
    {
        CRYPT_CIPHER_AES192_CTR,
        "8e73b0f7da0e6452c810f32b809079e562f8ead2522c6b7b",
        "f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff",
        0,
        0,
        0,
        "1abc932417521ca24f2b0459fe7e6e0b",
        "6bc1bee22e409f96e93d7e117393172a",
    },
    {
        CRYPT_CIPHER_AES256_CTR,
        "603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4",
        "f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff",
        0,
        0,
        0,
        "601ec313775789a5b7a7f504bbf3d228",
        "6bc1bee22e409f96e93d7e117393172a",
    },
    {
        CRYPT_CIPHER_AES128_CBC,
        "3348aa51e9a45c2dbe33ccc47f96e8de",
        "19153c673160df2b1d38c28060e59b96",
        1,
        0,
        0,
        "9b7cee827a26575afdbb7c7a329f887238052e3601a7917456ba61251c214763d5e1847a6ad5d54127a399ab07ee3599",
        "d5aed6c9622ec451a15db12819952b6752501cf05cdbf8cda34a457726ded97818e1f127a28d72db5652749f0c6afee5",
    },
    {
        CRYPT_CIPHER_AES128_CBC,
        "625eefa18a4756454e218d8bfed56e36",
        "73d9d0e27c2ec568fbc11f6a0998d7c8",
        0,
        0,
        0,
        "5d6fed86f0c4fe59a078d6361a142812514b295dc62ff5d608a42ea37614e6a1",
        "360dc1896ce601dfb2a949250067aad96737847a4580ede2654a329b842fe81e",
    },
    {
        CRYPT_CIPHER_AES192_CBC,
        "16c93bb398f1fc0cf6d68fc7a5673cdf431fa147852b4a2d",
        "eaaeca2e07ddedf562f94df63f0a650f",
        1,
        0,
        0,
        "c5ce958613bf741718c17444484ebaf1050ddcacb59b9590178cbe69d7ad7919608cb03af13bbe04f3506b718a301ea0",
        "ed6a50e0c6921d52d6647f75d67b4fd56ace1fedb8b5a6a997b4d131640547d22c5d884a75e6752b5846b5b33a5181f4",
    },
    {
        CRYPT_CIPHER_AES192_CBC,
        "3915d786c786731cfe35abe39fac714f5fa32c7ef3c6681b",
        "a2d326a8226576e32e48f62b3da96c40",
        0,
        0,
        0,
        "a9968021d6df78ff2c4c236bdd9a55bc727b0dc506f44958b2041f0948860a3444588242ffbdcf2726001e2f6b5bd5fb",
        "4a7a4dca5c555d3f0358be7db4af14f1322a8861a3cb977f029fdcbd8ee4a8d451f32d7865e6a2376edf67e4d1092e15",
    },
    {
        CRYPT_CIPHER_AES256_CBC,
        "0493ff637108af6a5b8e90ac1fdf035a3d4bafd1afb573be7ade9e8682e663e5",
        "c0cd2bebccbb6c49920bd5482ac756e8",
        1,
        0,
        0,
        "8b37f9148df4bb25956be6310c73c8dc58ea9714ff49b643107b34c9bff096a94fedd6823526abc27a8e0b16616eee254ab4567dd68e8ccd4c38ac563b13639c",
        "05d5c77729421b08b737e41119fa4438d1f570cc772a4d6c3df7ffeda0384ef84288ce37fc4c4c7d1125a499b051364c389fd639bdda647daa3bdadab2eb5594",
    },
    {
        CRYPT_CIPHER_AES256_CBC,
        "7482c47004aef406115ca5fd499788d582efc0b29dc9e951b1f959406693a54f",
        "485ebf2215d20b816ea53944829717ce",
        0,
        0,
        0,
        "6c24f19b9c0b18d7126bf68090cb8ae72db3ca7eabb594f506aae7a2493e5326a5afae4ec4d109375b56e2b6ff4c9cf639e72c63dc8114c796df95b3c6b62021",
        "82fec664466d585023821c2e39a0c43345669a41244d05018a23d7159515f8ff4d88b01cd0eb83070d0077e065d74d7373816b61505718f8d4f270286a59d45e",
    },
};

static void SDV_KAE_AES_ENCRYPT_FUNC_TC001(void **state)
{
    CRYPT_AES_Hex *hex = (CRYPT_AES_Hex *)*state;

    Hex *key = NewHex();
    Hex *iv = NewHex();
    Hex *in = NewHex();
    Hex *out = NewHex();

    assert_int_equal(ConvertHex(hex->keyHex, key), 0);
    assert_int_equal(ConvertHex(hex->ivHex, iv), 0);
    assert_int_equal(ConvertHex(hex->inHex, in), 0);
    assert_int_equal(ConvertHex(hex->outHex, out), 0);

    uint8_t outTmp[MAX_OUTPUT] = {0};
    uint32_t len = MAX_OUTPUT;
    uint32_t totalLen = 0;

    CRYPT_EAL_LibCtx *libctx = CreateLoadLibCtx();
    CRYPT_EAL_CipherCtx *ctx = CRYPT_EAL_ProviderCipherNewCtx(libctx, hex->id, "provider=kaev1");
    assert_non_null(ctx);

    assert_int_equal(CRYPT_EAL_CipherInit(ctx, key->x, key->len, iv->x, iv->len, hex->temp), CRYPT_SUCCESS);
    assert_int_equal(CRYPT_EAL_CipherUpdate(ctx, in->x, in->len, outTmp, &len), CRYPT_SUCCESS);
    totalLen += len;
    len = MAX_OUTPUT - len;
    assert_int_equal(CRYPT_EAL_CipherFinal(ctx, outTmp + totalLen, &len), CRYPT_SUCCESS);
    totalLen += len;
    assert_int_equal(totalLen, out->len);
    assert_int_equal(memcmp(outTmp, out->x, out->len), 0);

    FreeHex(key);
    FreeHex(iv);
    FreeHex(in);
    FreeHex(out);

    CRYPT_EAL_CipherFreeCtx(ctx);
    CRYPT_EAL_LibCtxFree(libctx);
}

static CRYPT_AES_Hex SDV_KAE_AES_ENCRYPT_FUNC_TC002_VEC[] = {
    {
        CRYPT_CIPHER_AES128_CBC,
        "8809e7dd3a959ee5d8dbb13f501f2274",
        "e5c0bb535d7d54572ad06d170a0e58ae",
        1,
        0,
        0,
        "1fd4ee65603e6130cfc2a82ab3d56c24",
        "b127a5b4c4692d87483db0c3b0d11e64",
    },
    {
        CRYPT_CIPHER_AES128_CBC,
        "287b07c78f8e3e1be7c41b3d96c04e6e",
        "41b461f9464fd515d25413b4241002b8",
        0,
        0,
        0,
        "7c54923b0490a9d4de4ec1ce6790aa4d",
        "2805d10b127fcd1da528faad4eb2e10b",
    },
    {
        CRYPT_CIPHER_AES192_CBC,
        "dea64f83cfe6a0a183ddbe865cfca059b3c615c1623d63fc",
        "426fbc087b50b395c0fc81ef9fd6d1aa",
        1,
        0,
        0,
        "cd0b8c8a8179ecb171b64c894a4d60fd",
        "ae6302d22da9458117f5681431fc80df",
    },
    {
        CRYPT_CIPHER_AES192_CBC,
        "a24ebd4d7a080c28caae984b5098a9ea38cf7280e2c5f122",
        "c5aeb9b51ad5108371c59d0b90816310",
        0,
        0,
        0,
        "eb2c4e2712591ff13b8ac7870c9c404c",
        "886dc6ee8774e7a5b378ac8a2b637e50",
    },
    {
        CRYPT_CIPHER_AES256_CBC,
        "632bac4fe4db44cfcf18cfa90b43f86f378611b8d968595eb89e7ae98624564a",
        "ff8127621be616803e3f002377730185",
        1,
        0,
        0,
        "90ed17475f0a62bc381ba1f3ffbfff33",
        "4494030b1e828f57e349cbde6499abf3",
    },
    {
        CRYPT_CIPHER_AES256_CBC,
        "31397ad8cc79c519e0f46e0f70303587e38958d70723b771552336b7771f6311",
        "4139cb54eeac3fcf36ed72941122c40f",
        0,
        0,
        0,
        "27a1d5c10fe45b801d15f56e654a70f0",
        "f0e50e036baf80cef566d3f9eaa2a9a7",
    },
    {
        CRYPT_CIPHER_AES128_OFB,
        "89f6806368c130627a98bfb6bb5b1fd7",
        "b24d13affed86717df32a5b43fa9b859",
        1,
        0,
        0,
        "056f3063170450a5ac72c15caa54c690",
        "b0e7562dac92f90d5935c57baf336204",
    },
    {
        CRYPT_CIPHER_AES128_OFB,
        "6ec8f8db4b5eec1f64a799c1bb29b9aa",
        "b2321765f48eddc7f84b9423a540003c",
        0,
        0,
        0,
        "0dda834adaec203904d288f43263c4b7",
        "9c1037b77f32bd314929cea95a39bcdc",
    },
    {
        CRYPT_CIPHER_AES192_OFB,
        "3264e48dc08e22e3d83f664c3a81a7b9a46da5b70093c2f2",
        "cd5db293836a44f55647eb8844ccad8d",
        1,
        0,
        0,
        "be25385d443c3eab7c6f4fb4eac7a5db",
        "394655a878ac85fee108f3cb5cbb4350",
    },
    {
        CRYPT_CIPHER_AES192_OFB,
        "c79765ec6ed647aa5023c730e71cde2989d225534820e078",
        "4f032ffe0b2ae69847987b82cd7caa99",
        0,
        0,
        0,
        "9983ce048f19ef4043054c03aa010bee",
        "280dab0a264179ddc7a5564518ddf641",
    },
    {
        CRYPT_CIPHER_AES256_OFB,
        "b46129976d3fb9a83b10d3f6bb928a5b2e8fd00ec10fdf7513ccd83af1e6aaa1",
        "39c6a3ab26ae170921c503fc62ec0ba9",
        1,
        0,
        0,
        "7a644b53ef2cb607328c63dd61321515",
        "0e865252917d7a10547480988fe3f80e",
    },
    {
        CRYPT_CIPHER_AES256_OFB,
        "8eda005a16f5d0cc62727e92ded412e8e7d9e01f98000043e049fffcd83cd4b2",
        "57d404ae6da97c9ed315a13739954cf9",
        1,
        0,
        0,
        "53de1c313745ecf39963059435541fba",
        "e68ee3c8cabb00e41be436499ac47d5c",

    },
};

static void SDV_KAE_AES_ENCRYPT_FUNC_TC002(void **state)
{
    CRYPT_AES_Hex *hex = (CRYPT_AES_Hex *)*state;

    Hex *key = NewHex();
    Hex *iv = NewHex();
    Hex *in = NewHex();
    Hex *out = NewHex();

    assert_int_equal(ConvertHex(hex->keyHex, key), 0);
    assert_int_equal(ConvertHex(hex->ivHex, iv), 0);
    assert_int_equal(ConvertHex(hex->inHex, in), 0);
    assert_int_equal(ConvertHex(hex->outHex, out), 0);

    uint32_t len = AES_BLOCKSIZE;
    uint8_t mctResult[MCT_INNER_LOOP][AES_BLOCKSIZE] = {0};
    uint8_t *inputTmp = in->x;

    CRYPT_EAL_LibCtx *libctx = CreateLoadLibCtx();
    CRYPT_EAL_CipherCtx *ctx = CRYPT_EAL_ProviderCipherNewCtx(libctx, hex->id, "provider=kaev1");
    assert_non_null(ctx);

    assert_int_equal(CRYPT_EAL_CipherInit(ctx, key->x, key->len, iv->x, iv->len, hex->temp), CRYPT_SUCCESS);
    for (uint32_t i = 0; i < MCT_INNER_LOOP; i++) {
        assert_int_equal(CRYPT_EAL_CipherUpdate(ctx, inputTmp, AES_BLOCKSIZE, mctResult[i], &len), CRYPT_SUCCESS);
        if (i == 0) {
            inputTmp = iv->x;
        } else {
            inputTmp = mctResult[i - 1];
        }
    }

    assert_int_equal(memcmp(mctResult[MCT_INNER_LOOP - 1], out->x, out->len), 0);

    FreeHex(key);
    FreeHex(iv);
    FreeHex(in);
    FreeHex(out);

    CRYPT_EAL_CipherFreeCtx(ctx);
    CRYPT_EAL_LibCtxFree(libctx);
}

static CRYPT_AES_Hex SDV_KAE_AES_ENCRYPT_FUNC_TC003_VEC[] = {
    {
        CRYPT_CIPHER_AES128_CBC,
        "3348aa51e9a45c2dbe33ccc47f96e8de",
        "19153c673160df2b1d38c28060e59b96",
        1,
        0,
        0,
        "9b7cee827a26575afdbb7c7a329f887238052e3601a7917456ba61251c214763d5e1847a6ad5d54127a399ab07ee3599",
        "d5aed6c9622ec451a15db12819952b6752501cf05cdbf8cda34a457726ded97818e1f127a28d72db5652749f0c6afee5",
    },
    {
        CRYPT_CIPHER_AES128_CBC,
        "625eefa18a4756454e218d8bfed56e36",
        "73d9d0e27c2ec568fbc11f6a0998d7c8",
        0,
        0,
        0,
        "5d6fed86f0c4fe59a078d6361a142812514b295dc62ff5d608a42ea37614e6a1",
        "360dc1896ce601dfb2a949250067aad96737847a4580ede2654a329b842fe81e",
    },
    {
        CRYPT_CIPHER_AES192_CBC,
        "ba75f4d1d9d7cf7f551445d56cc1a8ab2a078e15e049dc2c",
        "531ce78176401666aa30db94ec4a30eb",
        1,
        0,
        0,
        "c51fc276774dad94bcdc1d2891ec8668",
        "70dd95a14ee975e239df36ff4aee1d5d",
    },
    {
        CRYPT_CIPHER_AES192_CBC,
        "8e2740fba157aef2422e442312d15c14d312553684fcdc15",
        "324015878cdc82bfae59a2dc1ff34ea6",
        0,
        0,
        0,
        "39a9b42de19e512ab7f3043564c3515a",
        "aa41179d880e6fe3b14818d6e4a62eb5",
    },
    {
        CRYPT_CIPHER_AES256_CBC,
        "6ed76d2d97c69fd1339589523931f2a6cff554b15f738f21ec72dd97a7330907",
        "851e8764776e6796aab722dbb644ace8",
        1,
        0,
        0,
        "6282b8c05c5c1530b97d4816ca434762",
        "6acc04142e100a65f51b97adf5172c41",
    },
    {
        CRYPT_CIPHER_AES256_CBC,
        "54682728db5035eb04b79645c64a95606abb6ba392b6633d79173c027c5acf77",
        "2eb94297772851963dd39a1eb95d438f",
        0,
        0,
        0,
        "e4046d05385ab789c6a72866e08350f93f583e2a005ca0faecc32b5cfc323d461c76c107307654db5566a5bd693e227c",
        "0faa5d01b9afad3bb519575daaf4c60a5ed4ca2ba20c625bc4f08799addcf89d19796d1eff0bd790c622dc22c1094ec7",
    },
    {
        CRYPT_CIPHER_AES128_OFB,
        "cde9b69eea2b6a5588457e35e0a08803",
        "52323b54d69a62fec0689baee1b3ec63",
        1,
        0,
        0,
        "967798995af6f435b3a6f92bff77a11fa44d1426ae0f6e7dbafac27b123c5fc419be52c0ea412c4b3cac05ae89a4c0ce6f5e91a456b1bded5370a1234cf6f6ab5d0253507bc6f3f0573ab97585b67107dec059812323e021e341ad839ea9e3d02aeca43356add48ccef81f693ed53d32ba1c74a35e8a5f7f3115ef834f7daf9948244c4fc31f5487678d3e70fb27abb5",
        "6a5747276037643bbd0013c265d8d9a80b0299b283514d5256fecb5c787002a291a18a765fa046c3243418b02eebfc0c599576e52dd8c30291c97ceaa8bd2d7dbee3e66db7b585ea2b67f46f6711df28456b801556e233a96da1a8c34cd4d6154b20f43ae27b8ae83d907f9355c87aa021a280232265e99b4e189f4a3ccaa6b5e04153961e8e427a2dd53e5ec6f5112a",
    },
    {
        CRYPT_CIPHER_AES128_OFB,
        "01963d44aea026b2205238454d5bb73f",
        "9442a6e0f3a53f10b0ccf5b0ccc1793a",
        0,
        0,
        0,
        "a1452e63a52fd294009482289812735166117f5427a6759154b8b4be5561f873f29673eb0a0b200987515499914196d9029eb0371af6065d75c9276b39eea283",
        "c54cfacd953736a2d8db0b8b63b555253a0ca6f6e05f2e918d18be95669fa85609f827d6da014add2964626670c202b195248fc986372c92adbb10c0e7c36e04",
    },
    {
        CRYPT_CIPHER_AES192_OFB,
        "66c7d31359eac09056d597816542bffe4bb33e475dfb2d62",
        "426042dc81a7a069251972b91fb35058",
        1,
        0,
        0,
        "2f2178a285e61932c0b75d7be0a6e23afe78248330fc8bb3ad9ca9a73232bc2ba41d7bb5f6930f544d385fe362f0908228f2cc47b01f43304991705ceb769e7b",
        "863aa235c8ec3d7e8b24244f9eb797a610d0814cf15b2bdc2b17e90e02e15e2b4b73affc0d5983aa9e9b63fc5004629b1e337129cd3e4f3cc48b7f174544e30e",
    },
    {
        CRYPT_CIPHER_AES192_OFB,
        "6c98ed90354af277df6c64d3f4f7af72d94317959bbf9f46",
        "e80bbd08f6ed38755132a0d0c58cb765",
        0,
        0,
        0,
        "564af9b8c99e33a668c10f86a89fd55327b423009a2c85bb06494573340685985ffa8a222f7e5bd4eb128bc2eafd53114a34e4fee57dfa2181d4b5f3470ea58f",
        "40aa6ef1f783d1187c1fe998c08373ca9b3021b112f03bff57179f10bbb987750c22b1ed65a18340033d35a7527200dc244ade241989eac950892897c77c7f5d",
    },
    {
        CRYPT_CIPHER_AES256_OFB,
        "318aa7c73006ff95840f17f2b9cf01fe7f031105ff01daa66ff95834e47b6f5c",
        "0e28bd0603b31c26250345a118408ffc",
        1,
        0,
        0,
        "257f3fc84537158b68c8af111b1e9eb41f8841686ab1e94c6fd13a7f9f24d535309c340a1dd3d4966e439a41b9b97058e9072f613ef9c1ac958b872bea59f8831b578b63eec2d7155657f953f2c2375b",
        "ba4ebcdc894e6de54f8f1d7ccbb19e13d2ae0ca66c05c10e2f90bad2e9b8db94ee7770c3557927029d49fd2b3f80a01025af0e7a343237fb625dbdee85367ddfbd7f6664b511cdc7e832b2c4d91f1c0e",
    },
    {
        CRYPT_CIPHER_AES256_OFB,
        "75be95a6a54400b2e1b485e24ead18ed20a6261fba82d397166486b268c4a7c7",
        "3b395ad3450c56c8f1accab2a47be11e",
        0,
        0,
        0,
        "52647fa67c0675753f725a8fd446622a2dff2841e61a46dc79792491b1090d4b61a2884a552ee56b890e16aa99b9c0e579a692e3666dd6941c29459a4a202009",
        "6892fc5c3d48f4e68a2c318f8157ee986fcf6c85a2ece70c0fdc709c504f0ab0b91606c4f13bc9bce5f1a1e0ce46960be86c35bc5982418097ba282d887608ea",
    },
    {
        CRYPT_CIPHER_AES128_CTR,
        "2b7e151628aed2a6abf7158809cf4f3c",
        "f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff",
        1,
        0,
        0,
        "6bc1bee22e409f96e93d7e117393172a",
        "874d6191b620e3261bef6864990db6ce",
    },
    {
        CRYPT_CIPHER_AES128_CTR,
        "2b7e151628aed2a6abf7158809cf4f3c",
        "f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff",
        0,
        0,
        0,
        "874d6191b620e3261bef6864990db6ce",
        "6bc1bee22e409f96e93d7e117393172a",
    },
    {
        CRYPT_CIPHER_AES192_CTR,
        "8e73b0f7da0e6452c810f32b809079e562f8ead2522c6b7b",
        "f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff",
        1,
        0,
        0,
        "6bc1bee22e409f96e93d7e117393172a",
        "1abc932417521ca24f2b0459fe7e6e0b",
    },
    {
        CRYPT_CIPHER_AES192_CTR,
        "8e73b0f7da0e6452c810f32b809079e562f8ead2522c6b7b",
        "f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff",
        0,
        0,
        0,
        "1abc932417521ca24f2b0459fe7e6e0b",
        "6bc1bee22e409f96e93d7e117393172a",
    },
    {
        CRYPT_CIPHER_AES256_CTR,
        "603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4",
        "f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff",
        1,
        0,
        0,
        "6bc1bee22e409f96e93d7e117393172a",
        "601ec313775789a5b7a7f504bbf3d228",
    },
    {
        CRYPT_CIPHER_AES256_CTR,
        "603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4",
        "f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff",
        0,
        0,
        0,
        "601ec313775789a5b7a7f504bbf3d228",
        "6bc1bee22e409f96e93d7e117393172a",
    },
};

static void SDV_KAE_AES_ENCRYPT_FUNC_TC003(void **state)
{
    CRYPT_AES_Hex *hex = (CRYPT_AES_Hex *)*state;

    Hex *key = NewHex();
    Hex *iv = NewHex();
    Hex *in = NewHex();
    Hex *out = NewHex();

    assert_int_equal(ConvertHex(hex->keyHex, key), 0);
    assert_int_equal(ConvertHex(hex->ivHex, iv), 0);
    assert_int_equal(ConvertHex(hex->inHex, in), 0);
    assert_int_equal(ConvertHex(hex->outHex, out), 0);

    uint8_t outTmp[MAX_OUTPUT] = {0};
    uint32_t len = MAX_OUTPUT;
    uint32_t finLen;

    CRYPT_EAL_LibCtx *libctx = CreateLoadLibCtx();
    CRYPT_EAL_CipherCtx *ctx = CRYPT_EAL_ProviderCipherNewCtx(libctx, hex->id, "provider=kaev1");
    assert_non_null(ctx);

    assert_int_equal(CRYPT_EAL_CipherInit(ctx, key->x, key->len, iv->x, iv->len, hex->temp), CRYPT_SUCCESS);
    assert_int_equal(CRYPT_EAL_CipherUpdate(ctx, in->x, in->len, outTmp, &len), CRYPT_SUCCESS);
    finLen = MAX_OUTPUT - len;
    assert_int_equal(CRYPT_EAL_CipherFinal(ctx, outTmp + len, &finLen), CRYPT_SUCCESS);
    assert_memory_equal(outTmp, out->x, out->len);

    (void)memset_s(outTmp, MAX_OUTPUT, 0, MAX_OUTPUT);
    len = MAX_OUTPUT;
    assert_int_equal(CRYPT_EAL_CipherReinit(ctx, iv->x, iv->len), CRYPT_SUCCESS);
    assert_int_equal(CRYPT_EAL_CipherUpdate(ctx, in->x, in->len, outTmp, &len), CRYPT_SUCCESS);
    finLen = MAX_OUTPUT - len;
    assert_int_equal(CRYPT_EAL_CipherFinal(ctx, outTmp + len, &finLen), CRYPT_SUCCESS);
    assert_memory_equal(outTmp, out->x, out->len);

    FreeHex(key);
    FreeHex(iv);
    FreeHex(in);
    FreeHex(out);

    CRYPT_EAL_CipherFreeCtx(ctx);
    CRYPT_EAL_LibCtxFree(libctx);
}

static CRYPT_AES_Hex SDV_KAE_AES_ENCRYPT_FUNC_TC004_VEC[] = {
    {
        CRYPT_CIPHER_AES128_CBC,
        "3348aa51e9a45c2dbe33ccc47f96e8de",
        "19153c673160df2b1d38c28060e59b96",
        0,
        CRYPT_PADDING_NONE,
        1,
        "9b7cee827a26575afdbb7c7a329f887238052e3601a7917456ba61251c214763d5e1847a6ad5d54127a399ab07ee3599",
        NULL,
    },
    {
        CRYPT_CIPHER_AES128_CBC,
        "3348aa51e9a45c2dbe33ccc47f96e8de",
        "19153c673160df2b1d38c28060e59b96",
        0,
        CRYPT_PADDING_ZEROS,
        1,
        "9b7cee827a26575afdbb7c7a329f887238052e3601a7917456ba61251c214763d5",
        NULL,
    },
    {
        CRYPT_CIPHER_AES128_CBC,
        "3348aa51e9a45c2dbe33ccc47f96e8de",
        "19153c673160df2b1d38c28060e59b96",
        0,
        CRYPT_PADDING_ISO7816,
        1,
        "9b7cee827a26575afdbb7c7a329f887238052e3601a7917456ba61251c214763d5",
        NULL,
    },
    {
        CRYPT_CIPHER_AES128_CBC,
        "3348aa51e9a45c2dbe33ccc47f96e8de",
        "19153c673160df2b1d38c28060e59b96",
        0,
        CRYPT_PADDING_X923,
        1,
        "9b7cee827a26575afdbb7c7a329f887238052e3601a7917456ba61251c214763d5",
        NULL,
    },
    {
        CRYPT_CIPHER_AES128_CBC,
        "3348aa51e9a45c2dbe33ccc47f96e8de",
        "19153c673160df2b1d38c28060e59b96",
        0,
        CRYPT_PADDING_PKCS5,
        1,
        "9b7cee827a26575afdbb7c7a329f887238052e3601a7917456ba61251c214763d5",
        NULL,
    },
    {
        CRYPT_CIPHER_AES128_CBC,
        "3348aa51e9a45c2dbe33ccc47f96e8de",
        "19153c673160df2b1d38c28060e59b96",
        0,
        CRYPT_PADDING_PKCS7,
        1,
        "9b7cee827a26575afdbb7c7a329f887238052e3601a7917456ba61251c214763d5",
        NULL,
    },
    {
        CRYPT_CIPHER_AES192_CBC,
        "16c93bb398f1fc0cf6d68fc7a5673cdf431fa147852b4a2d",
        "eaaeca2e07ddedf562f94df63f0a650f",
        0,
        CRYPT_PADDING_NONE,
        1,
        "c5ce958613bf741718c17444484ebaf1050ddcacb59b9590178cbe69d7ad7919608cb03af13bbe04f3506b718a301ea0",
        NULL,
    },
    {
        CRYPT_CIPHER_AES192_CBC,
        "16c93bb398f1fc0cf6d68fc7a5673cdf431fa147852b4a2d",
        "eaaeca2e07ddedf562f94df63f0a650f",
        0,
        CRYPT_PADDING_ZEROS,
        1,
        "c5ce958613bf741718c17444484ebaf1050ddcacb59b959017",
        NULL,
    },
    {
        CRYPT_CIPHER_AES192_CBC,
        "16c93bb398f1fc0cf6d68fc7a5673cdf431fa147852b4a2d",
        "eaaeca2e07ddedf562f94df63f0a650f",
        0,
        CRYPT_PADDING_ISO7816,
        1,
        "c5ce958613bf741718c17444484ebaf1050ddcacb59b959017",
        NULL,
    },
    {
        CRYPT_CIPHER_AES192_CBC,
        "16c93bb398f1fc0cf6d68fc7a5673cdf431fa147852b4a2d",
        "eaaeca2e07ddedf562f94df63f0a650f",
        0,
        CRYPT_PADDING_X923,
        1,
        "c5ce958613bf741718c17444484ebaf1050ddcacb59b959017",
        NULL,
    },
    {
        CRYPT_CIPHER_AES192_CBC,
        "16c93bb398f1fc0cf6d68fc7a5673cdf431fa147852b4a2d",
        "eaaeca2e07ddedf562f94df63f0a650f",
        0,
        CRYPT_PADDING_PKCS5,
        1,
        "c5ce958613bf741718c17444484ebaf1050ddcacb59b959017",
        NULL,
    },
    {
        CRYPT_CIPHER_AES192_CBC,
        "16c93bb398f1fc0cf6d68fc7a5673cdf431fa147852b4a2d",
        "eaaeca2e07ddedf562f94df63f0a650f",
        0,
        CRYPT_PADDING_PKCS7,
        1,
        "c5ce958613bf741718c17444484ebaf1050ddcacb59b959017",
        NULL,
    },
    {
        CRYPT_CIPHER_AES256_CBC,
        "0493ff637108af6a5b8e90ac1fdf035a3d4bafd1afb573be7ade9e8682e663e5",
        "c0cd2bebccbb6c49920bd5482ac756e8",
        0,
        CRYPT_PADDING_NONE,
        1,
        "8b37f9148df4bb25956be6310c73c8dc58ea9714ff49b643107b34c9bff096a94fedd6823526abc27a8e0b16616eee254ab4567dd68e8ccd4c38ac563b13639c",
        NULL,
    },
    {
        CRYPT_CIPHER_AES256_CBC,
        "0493ff637108af6a5b8e90ac1fdf035a3d4bafd1afb573be7ade9e8682e663e5",
        "c0cd2bebccbb6c49920bd5482ac756e8",
        0,
        CRYPT_PADDING_ZEROS,
        1,
        "8b37f9148df4bb25956be6310c73c8dc58ea9714ff49b643107b34c9bff096a94fedd6823526abc27a8e0b16616eee254ab4",
        NULL,
    },
    {
        CRYPT_CIPHER_AES256_CBC,
        "0493ff637108af6a5b8e90ac1fdf035a3d4bafd1afb573be7ade9e8682e663e5",
        "c0cd2bebccbb6c49920bd5482ac756e8",
        0,
        CRYPT_PADDING_ISO7816,
        1,
        "8b37f9148df4bb25956be6310c73c8dc58ea9714ff49b643107b34c9bff096a94fedd6823526abc27a8e0b16616eee254ab4",
        NULL,
    },
    {
        CRYPT_CIPHER_AES256_CBC,
        "0493ff637108af6a5b8e90ac1fdf035a3d4bafd1afb573be7ade9e8682e663e5",
        "c0cd2bebccbb6c49920bd5482ac756e8",
        0,
        CRYPT_PADDING_X923,
        1,
        "8b37f9148df4bb25956be6310c73c8dc58ea9714ff49b643107b34c9bff096a94fedd6823526abc27a8e0b16616eee254ab4",
        NULL,
    },
    {
        CRYPT_CIPHER_AES256_CBC,
        "0493ff637108af6a5b8e90ac1fdf035a3d4bafd1afb573be7ade9e8682e663e5",
        "c0cd2bebccbb6c49920bd5482ac756e8",
        0,
        CRYPT_PADDING_PKCS5,
        1,
        "8b37f9148df4bb25956be6310c73c8dc58ea9714ff49b643107b34c9bff096a94fedd6823526abc27a8e0b16616eee254ab4",
        NULL,
    },
    {
        CRYPT_CIPHER_AES256_CBC,
        "0493ff637108af6a5b8e90ac1fdf035a3d4bafd1afb573be7ade9e8682e663e5",
        "c0cd2bebccbb6c49920bd5482ac756e8",
        0,
        CRYPT_PADDING_PKCS7,
        1,
        "8b37f9148df4bb25956be6310c73c8dc58ea9714ff49b643107b34c9bff096a94fedd6823526abc27a8e0b16616eee254ab4",
        NULL,
    },
};

static void SDV_KAE_AES_ENCRYPT_FUNC_TC004(void **state)
{
    CRYPT_AES_Hex *hex = (CRYPT_AES_Hex *)*state;

    Hex *key = NewHex();
    Hex *iv = NewHex();
    Hex *in = NewHex();

    assert_int_equal(ConvertHex(hex->keyHex, key), 0);
    assert_int_equal(ConvertHex(hex->ivHex, iv), 0);
    assert_int_equal(ConvertHex(hex->inHex, in), 0);

    uint8_t outTmp[MAX_OUTPUT] = {0};
    uint8_t result[MAX_OUTPUT] = {0};
    uint32_t totalLen = 0;
    uint32_t leftLen = MAX_OUTPUT;
    uint32_t len = MAX_OUTPUT;

    CRYPT_EAL_LibCtx *libctx = CreateLoadLibCtx();
    CRYPT_EAL_CipherCtx *ctxEnc = CRYPT_EAL_ProviderCipherNewCtx(libctx, hex->id, "provider=kaev1");
    assert_non_null(ctxEnc);

    assert_int_equal(CRYPT_EAL_CipherSetPadding(ctxEnc, hex->padding), CRYPT_SUCCESS);
    assert_int_equal(CRYPT_EAL_CipherInit(ctxEnc, key->x, key->len, iv->x, iv->len, true), CRYPT_SUCCESS);
    assert_int_equal(CRYPT_EAL_CipherSetPadding(ctxEnc, hex->padding), CRYPT_SUCCESS);
    assert_int_equal(CRYPT_EAL_CipherUpdate(ctxEnc, in->x, in->len, outTmp, &len), CRYPT_SUCCESS);
    totalLen += len;
    leftLen = leftLen - len;
    assert_int_equal(CRYPT_EAL_CipherFinal(ctxEnc, outTmp + len, &leftLen), CRYPT_SUCCESS);
    totalLen += leftLen;

    len = MAX_OUTPUT;
    leftLen = MAX_OUTPUT;

    CRYPT_EAL_CipherCtx *ctxDec = CRYPT_EAL_ProviderCipherNewCtx(libctx, hex->id, "provider=kaev1");
    assert_non_null(ctxDec);

    assert_int_equal(CRYPT_EAL_CipherInit(ctxDec, key->x, key->len, iv->x, iv->len, false), CRYPT_SUCCESS);
    assert_int_equal(CRYPT_EAL_CipherSetPadding(ctxDec, hex->padding), CRYPT_SUCCESS);
    assert_int_equal(CRYPT_EAL_CipherUpdate(ctxDec, outTmp, totalLen, result, &len), CRYPT_SUCCESS);
    leftLen -= len;
    assert_int_equal(CRYPT_EAL_CipherFinal(ctxDec, result + len, &leftLen), CRYPT_SUCCESS);

    assert_memory_equal(in->x, result, in->len);

    FreeHex(key);
    FreeHex(iv);
    FreeHex(in);

    CRYPT_EAL_CipherFreeCtx(ctxEnc);
    CRYPT_EAL_CipherFreeCtx(ctxDec);
    CRYPT_EAL_LibCtxFree(libctx);
}

static CRYPT_AES_MultiData
    SDV_KAE_AES_ENCRYPT_FUNC_TC005_VEC
        [] =
            {
                {CRYPT_CIPHER_AES128_CBC, "2c14413751c31e2730570ba3361c786b", "1dbbeb2f19abb448af849796244a19d7",
                 "40d930f9a05334d9816fe204999c3f82a03f6a0457a8c475c94553d1d116693adc618049f0a769a2eed6a6cb14c0143ec5cccdbc8dec4ce560cfd206225709",
                 "326d4de7948e54d603d01b12d7fed752fb23f1aa4494fbb00130e9ded4e77e37",
                 "c079042d828040c325b1a5efd15fc842e44014ca4374bf38f3c3fc3ee327733b0c8aee1abcd055772f18dc04603f7b2c1ea69ff662361f2be0a171bbdcea1e5d3f",
                 "6be8a12800455a320538853e0cba31bd2d80ea0c85164a4c5c261ae485417d93effe2ebc0d0a0b51d6ea18633d210cf63c0c4ddbc27607f2e81ed9113191ef86d56f3b99be6c415a4150299fb846ce7160b40b63baf1179d19275a2e83698376d28b92548c68e06e6d994e2c1501ed297014e702cdefee2f656447706009614d801de1caaf73f8b7fa56cf1ba94b631933bbe577624380850f117435a0355b2b",
                 true},
                {CRYPT_CIPHER_AES128_CBC, "97a1025529b9925e25bbe78770ca2f99", "d4b4eab92aa9637e87d366384ed6915c",
                 "22cdc3306fcd4d31ccd32720cbb61bad28d855670657c48c7b88c31f4fa1f93c01b57da90be63ead67d6a325525e6ed45083e6fb70a53529d1fa0f55653b942af59d78a2660361d63a7290155ac5c43312a25b235dacbb",
                 "c863faf00940c99624076dfa44068e7c554c9038176953e571",
                 "751dfc0954d41d113771b06466b1c8d13e0d4cb675ed58d1a619e1540970983781dc11d2dd8525ab5745958d615defda",
                 "e8b89150d8438bf5b17449d6ed26bd72127e10e4aa57cad85283e8359e089208e84921649f5b60ea21f7867cbc9620560c4c6238db021216db453c9943f1f1a60546173daef2557c3cdd855031b353d4bf176f28439e48785c37d38f270aa4a6faad2baabcb0c0b2d1dd5322937498ce803ba1148440a52e227ddba4872fe4d81d2d76a939d24755adb8a7b8452ceed2d179e1a5848f316f5c016300a390bfa7",
                 false},
                {CRYPT_CIPHER_AES192_CBC, "162ad50ee64a0702aa551f571dedc16b2c1b6a1e4d4b5eee",
                 "24408038161a2ccae07b029bb66355c1",
                 "be8abf00901363987a82cc77d0ec91697ba3857f9e4f84bd79406c138d02698f003276d0449120bef4578d78",
                 "fecabe8e070e11710b3f0a2744bd52434ec70015884c181ebdfd",
                 "51c604a71c52e4c0e110bc408cd462b248a80b8a8ac06bb952ac1d7faed144807f1a731b7febcaf7835762defe92eccfc7a9944e1c702cffe6bc86733ed321423121085ac02df8962bcbc1937092eebf0e90a8b20e3dd8c244ae",
                 "c82cf2c476dea8cb6a6e607a40d2f0391be82ea9ec84a537a6820f9afb997b76397d005424faa6a74dc4e8c7aa4a8900690f894b6d1dca80675393d2243adac762f159301e357e98b724762310cd5a7bafe1c2a030dba46fd93a9fdb89cc132ca9c17dc72031ec6822ee5a9d99dbca66c784c01b0885cbb62e29d97801927ec415a5d215158d325f9ee689437ad1b7684ad33c0d92739451ac87f39ff8c31b84",
                 true},
                {CRYPT_CIPHER_AES192_CBC, "c6680fe9a1968f899479eca1092beaac18ad945a42ce8f93",
                 "54130eea9e96a1199d3c090f690a479f",
                 "8a21ea1381284bcdf818c2d4dfa976c13e5a3c253164ba1d30eccc27947c263457b43bff1c3d5e9c6fff27544d9419b0e7fc81d4a392a10e643e0eaf0bed571a3e3ee71a687e2d7900d7face0fc42a96ecc886864a",
                 "60e9207536a285d9971a8ac427b70d6dd4ff8a340801e92b23f09ad62812e42fb6d87aed3b4f500664",
                 "b7ac73d8708033251ef792fa054eab98b5e5",
                 "c970a819cfb715f777e8b63167999ebe17c71ff505c3ff24cc6995430fad4013e1fc69ba5123072a7123e376d1f7de8cc610ada3fdd905a1476bc23724861e85dcf950db2b4982b60271752b49e438a20ef4e8e09cac0dc49ed15b84e32627e243814fee0430744ac675c7e5673d3f57a52360ec6ff8d18ed4b5bd8f1456c1f688825cb999789cad5e1b37a4b92ace3b",
                 false},
                {CRYPT_CIPHER_AES256_CBC, "0493ff637108af6a5b8e90ac1fdf035a3d4bafd1afb573be7ade9e8682e663e5",
                 "c0cd2bebccbb6c49920bd5482ac756e8", "8b37f9148df4bb25956be6310c73c8dc58ea9714ff49b643107b34",
                 "c9bff096a94fedd6823526abc27a8e0b16", "616eee254ab4567dd68e8ccd4c38ac563b13639c",
                 "05d5c77729421b08b737e41119fa4438d1f570cc772a4d6c3df7ffeda0384ef84288ce37fc4c4c7d1125a499b051364c389fd639bdda647daa3bdadab2eb5594",
                 true},
                {CRYPT_CIPHER_AES256_CBC, "d30bfc0b2a19d5b8b6f8f46ab7f444ee136a7fa3fbdaf530cc3e8976339afcc4",
                 "80be76a7f885d2c06b37d6a528fae0cd",
                 "31e4677a17aed120bd3af69fbb0e4b645b9e8c104e280b799ddd49f1e241c3ccb7d40e1c6ff226bf04f8049c51a86e2981cf1331c824d7d451746ccf77fc22fd3717001ee51913",
                 "d81f7a06fb0037f30995", "7579f695670f2c4c7397d2d990374e",
                 "0b6e2a8213169b3b78db6de324e286f0366044e035c6970afbf0a1a5c32a05b24ba706cd9c6609737651a81b2bcf4c681dc0861983a5aec76e6c8b244112d64d489e84328974737394b83a39459011727162652b7aa793bfb1b71488b7dec96b",
                 false},
                {CRYPT_CIPHER_AES128_CTR, "2b7e151628aed2a6abf7158809cf4f3c", "f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff",
                 "6bc1bee2", "2e409f96e93d7e11", "7393172a", "874d6191b620e3261bef6864990db6ce", true},
                {CRYPT_CIPHER_AES128_CTR, "2b7e151628aed2a6abf7158809cf4f3c", "f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff",
                 "874d6191b620e326", "1bef", "6864990db6ce", "6bc1bee22e409f96e93d7e117393172a", false},
                {CRYPT_CIPHER_AES192_CTR, "8e73b0f7da0e6452c810f32b809079e562f8ead2522c6b7b",
                 "f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff", "6bc1bee22e409f96e93d7e117393172a",
                 "ae2d8a571e03ac9c9eb76fac45af8e51", "30c81c46a35ce411e5fbc1191a0a52ef",
                 "1abc932417521ca24f2b0459fe7e6e0b090339ec0aa6faefd5ccc2c6f4ce8e941e36b26bd1ebc670d1bd1d665620abf7",
                 true},
                {CRYPT_CIPHER_AES192_CTR, "8e73b0f7da0e6452c810f32b809079e562f8ead2522c6b7b",
                 "f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff", "1abc932417521ca24f2b0459fe7e6e0b",
                 "090339ec0aa6faefd5ccc2c6f4ce8e94", "1e36b26bd1ebc670d1bd1d665620abf7",
                 "6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e5130c81c46a35ce411e5fbc1191a0a52ef",
                 false},
                {CRYPT_CIPHER_AES256_CTR, "603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4",
                 "f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff", "6bc1bee22e", "409f96e93d", "7e117393172a",
                 "601ec313775789a5b7a7f504bbf3d228", true},
                {CRYPT_CIPHER_AES256_CTR, "603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4",
                 "f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff", "601ec3137757", "89a5b7a7f5", "04bbf3d228",
                 "6bc1bee22e409f96e93d7e117393172a", false},
                {CRYPT_CIPHER_AES128_OFB, "939aac71e337709855715a57e3a4648f", "493509b56a92f14040eb9b66a188bc57",
                 "9c22efddc7de496a916d15d710de374d57478126ed64c9ad7e823e24d19bfc0cfac3dda0d1c292a3a203f35b26ad94deb20f998caf41cb",
                 "dd4a08eb5d6cfb46f4ede4896b0569d72c03ec1949",
                 "41af95c0573cc3fe8f045ba19946b382803248f3dd4f9a454b1a3e8e1af02ea8482d637dac96a68275f4a382d3023f9df4892b9032cab9378b1cef5051d6db81226f259d1be4eb23495ac807600536b5b0481754",
                 "7c0217d4f990342be5a35e2bdd4756ae7f461add633a7b0f5174ee107a7c0c53b1c787cb83e5ddb876e251a23caf7959d952638c2aa28b2b08928c9b88e4c0e0fd0d8154690c3638ce692f20905e7263ff359bcc17e3b43d2276ef1fc4c882282f9a453bc03eb29e9c95986318c19150acf1bf33270752d32488543f598f8ed4db3ccb990c8bfdf64cae0d1c6011042acda8c2687a758c2ba8080720990be88d",
                 true},
                {CRYPT_CIPHER_AES128_OFB, "e30b4c874c4c4f6e0cf1f8ef58e5d375", "7e26f07f8024343cec35409e71e0cd8c",
                 "5dcaa173ede14fd2d658973926168ff34fd6df9bce3280d40b00c43b80e2979a1e19045fec9afb4cf264516f55100855c3aad17b11bfcf0523b79eb20d65941077dd46ec46864e0d79704c2250e72bf8b448",
                 "a6f0d3130ab10b423d1a09d9ff7a32bf700441ccd27d3223913860c28044ea5766e45a55b93f8948a959bd6661421566898e",
                 "27950f04e726279bcbc990a22c80193ef0ae65196671eb59713240cf",
                 "8ceca4dc346cfd6b15774e082db1a89497b7d85d6b5b7102e77417f7a243fafe17118b7a3bb49d1657cf61b866da395a5b3f349183a53dfa11fc0ac053bddff49dd472ee55f5e43a2f8bc785e2bc420300694919ff7bb43feb75a9cac44ece96f679e618db5d7433af12dcc7e0963ff10b45d835f9a8f42627e7f3fd5038932685965ad0e183f5955e671fc2b878dd51051eedaf85310d1e4e8f75f2decf36c7",
                 false},
                {CRYPT_CIPHER_AES192_OFB, "17b0f9915f6b541e1d3fec5b9c5dfe1cb05a9dbc9983b20f",
                 "eaf17cec9714d2e6f266f283618494eb",
                 "0d4e7f3f732bf9d8d2d3f648968fe84175d1c7f5ac6d6be48c0539f336c501bfa3512730e7fc0151b63b815f27591420b86cc9759287b6330f31982f7a16b99816fd",
                 "178a61fac2df99e58649800aca9e5d22b87243839eeb959394d1ca8260", "e56e399674698d042b84b94c2d290bd3636addda1346c7ebb527137702ae71bd4db3eced16881d8edd7e9f1d34abc3f718ea84798122bd6538cc9987e9af4b9979", "05f6ec0f2e2215cc4518750c4c6adb0b4e0b28b0889b33528cc3865a44d8f3680d838ff3da8d57d18df22187a716ad24630645732b7510aa77f5e3181f402b72df2543f825ff06ad5993324524a917093867ae5b59ae439697fd53fb9605eed3cc6b5c89fee0b6bdfe62e1444290f3dd71ff6aa1b60a8abdbc70b2ae3aee999739d7e6952a18fcf43151b65a5c9504fe3bf166917a264ab0c048cdfc40d4e0bf",
                 true},
                {CRYPT_CIPHER_AES192_OFB, "753c1f7f39afd286ff051339c45d57c848850393b4112fe5",
                 "d3ee0d8de5080eda4c26f5a3b9ca8a9f",
                 "a91409fa694a4ad34a9cb1c1534a94979a5beb390083a6a61b4a4436746ba120f37a0f3e97caa9156a8ee410b53e670c703d1d19fcb8887f15d1580002fd3c5d0eaaf81c3c26b887",
                 "37bca3f88820b4540b0dc5d6a42ced0e2e380c29b460a472e4eceb19c0241e3397",
                 "6d170334b6227855120df65fa67e5a4fc68938f45b82665c8810bf8c8d832173eb68b821c95a7f35961b0394f0e36b61c4f7b529b77e42",
                 "ad3ccda264343130bf1db3703e27127176fbaa7b6a5da2718783baf9f28fcdd3ed9cd31adcc79427fe4df03f1672a5e55cab0db0f4d434d860340d2fa05bfeb07e924157064a24d0f10e3293f78a2676e3c53734f22d4ea33e89384bd17f4a0f59354179ce48a7d1c1ba35e7f77735f58680f0e89bf9242a4f087322d99e507336a8e9037b6f1bfbe45614abeb2f71516b94caf618851ddcfac7429a2177be40",
                 false},
                {CRYPT_CIPHER_AES256_OFB, "1d6fab8e8e49d623cfc8d105f04dcfab60175d7db4fd452ea463b34c56679615",
                 "2aff323645f8aecf9204e7450952264f",
                 "581d511bbd441d3531e77a910ad194f40d8a69b63cd6396cef7f37c5265485b21e725fca22c297eca4c341268c8aafd5d007a00e4589b43d",
                 "b584230c3193af87a8b9d77db9ec4e29c6d4ab114ad0622f6c3af34f",
                 "d4ec61b8bd02e60e4be2e5771f7a20fd2ac92b34bea1211cebab954808abcb40900528253081e8931d4b0fcbab3ea2121c611654b5b2090d1823306acde391ab22def2cb358791634fd515cc",
                 "6ca579544f243cb2074feb19edc128faa33635d6eed0c850502669860d7ea66842298154ab455f79db45fd72e0ce88d49210226c489e9c15fe09216218707bcb96e1e59aa8d7fcd99728f71a478f9fedc109a111622a63b8e6e736207b37adbf0e6f4990ed76b42434473f90bb8c75ca7df47d72dab61fb0c67d265502217c4e590b3536d8d7c7a03a64735e87799423f40b123b8e431e3bba45bf193b6c4af0",
                 true},
                {CRYPT_CIPHER_AES256_OFB, "98a9971e86806ccc3495116fd06dc9d1522fe88060fdddc36e846fd329d24748",
                 "9983ce048f19ef4043054c03aa010bee",
                 "4b0767fec0d4bd07c79c0f5652dafadf10cfce89e3259dea94d39252c640840c28abcbe0efca53dc84b1ef5579f6ef",
                 "28c213f445220b036fe351d93dcae57e654ac01d39f8721372",
                 "3f0a462d5536b8336dab5c7d2fc728f865756b85f7526144190e0412c3142650616dbde7cde17e887a60ad39f2a1330d8209f13233ce5431fd5c297238f8b3ff53a3fb89c84168b04ffa8f7f53e14c36a2d3124d68a27fb5",
                 "00caa233198f51bbf593404f59826997b4ea387385cf744c93cfa00e702e8f16ff5aa7e17a9a6020df0f0de4ea6abb38bcf1d777810a8318f69b5e8305f6d727f06f008b4bec2d65cd4c516ca49f62fb2f916f273c45bb722bec78c316f90b5ed5de6ef1d366603ced303c10e33dd5c99eb0f994db5a7867da9b530fc4d0b9ce224c6eab7810359c9733cf933c573611d31fcdf3f1db87cfd17be7f4a470a0b4",
                 false},
};

static void SDV_KAE_AES_ENCRYPT_FUNC_TC005(void **state)
{
    CRYPT_AES_MultiData *hex = (CRYPT_AES_MultiData *)*state;

    Hex *key = NewHex();
    Hex *iv = NewHex();
    Hex *in1 = NewHex();
    Hex *in2 = NewHex();
    Hex *in3 = NewHex();
    Hex *out = NewHex();

    assert_int_equal(ConvertHex(hex->keyHex, key), 0);
    assert_int_equal(ConvertHex(hex->ivHex, iv), 0);
    assert_int_equal(ConvertHex(hex->inPartAHex, in1), 0);
    assert_int_equal(ConvertHex(hex->inPartBHex, in2), 0);
    assert_int_equal(ConvertHex(hex->inPartCHex, in3), 0);
    assert_int_equal(ConvertHex(hex->outHex, out), 0);

    uint8_t outTmp[MAX_OUTPUT] = {0};
    uint32_t len = MAX_OUTPUT;
    uint32_t totalLen = 0;

    CRYPT_EAL_LibCtx *libctx = CreateLoadLibCtx();
    CRYPT_EAL_CipherCtx *ctx = CRYPT_EAL_ProviderCipherNewCtx(libctx, hex->id, "provider=kaev1");
    assert_non_null(ctx);

    assert_int_equal(CRYPT_EAL_CipherInit(ctx, key->x, key->len, iv->x, iv->len, hex->enc), CRYPT_SUCCESS);
    assert_int_equal(CRYPT_EAL_CipherUpdate(ctx, in1->x, in1->len, outTmp, &len), CRYPT_SUCCESS);
    totalLen += len;
    len = MAX_OUTPUT - totalLen;
    assert_int_equal(CRYPT_EAL_CipherUpdate(ctx, in2->x, in2->len, outTmp + totalLen, &len), CRYPT_SUCCESS);
    totalLen += len;
    len = MAX_OUTPUT - totalLen;
    assert_int_equal(CRYPT_EAL_CipherUpdate(ctx, in3->x, in3->len, outTmp + totalLen, &len), CRYPT_SUCCESS);
    totalLen += len;
    len = MAX_OUTPUT - totalLen;
    assert_int_equal(CRYPT_EAL_CipherFinal(ctx, outTmp + totalLen, &len), CRYPT_SUCCESS);
    assert_memory_equal(outTmp, out->x, out->len);

    FreeHex(key);
    FreeHex(iv);
    FreeHex(in1);
    FreeHex(in2);
    FreeHex(in3);
    FreeHex(out);

    CRYPT_EAL_CipherFreeCtx(ctx);
    CRYPT_EAL_LibCtxFree(libctx);
}

static CRYPT_AES_Hex SDV_KAE_AES_ENCRYPT_FUNC_TC006_VEC[] = {
    {
        CRYPT_CIPHER_AES256_CBC,
        "dce26c6b4cfb286510da4eecd2cffe6cdf430f33db9b5f77b460679bd49d13ae",
        "fdeaa134c8d7379d457175fd1a57d3fc",
        1,
        CRYPT_PADDING_NONE,
        0,
        "50e9eee1ac528009e8cbcd356975881f957254b13f91d7c6662d10312052eb00",
        "2fa0df722a9fd3b64cb18fb2b3db55ff2267422757289413f8f657507412a64c",
    },
    {
        CRYPT_CIPHER_AES256_CBC,
        "addf88c1ab997eb58c0455288c3a4fa320ada8c18a69cc90aa99c73b174dfde6",
        "60cc50e0887532e0d4f3d2f20c3c5d58",
        0,
        CRYPT_PADDING_NONE,
        0,
        "6cb4e2f4ddf79a8e08c96c7f4040e8a83266c07fc88dd0074ee25b00d445985a",
        "98a8a9d84356bf403a9ccc384a06fe043dfeecb89e59ce0cb8bd0a495ef76cf0",
    },
    {
        CRYPT_CIPHER_AES256_CTR,
        "603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4",
        "f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff",
        1,
        CRYPT_PADDING_NONE,
        0,
        "6bc1bee22e409f96e93d7e117393172a",
        "601ec313775789a5b7a7f504bbf3d228",
    },
    {
        CRYPT_CIPHER_AES256_CTR,
        "603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4",
        "f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff",
        0,
        CRYPT_PADDING_NONE,
        0,
        "601ec313775789a5b7a7f504bbf3d228",
        "6bc1bee22e409f96e93d7e117393172a",
    },
    {
        CRYPT_CIPHER_AES256_OFB,
        "318aa7c73006ff95840f17f2b9cf01fe7f031105ff01daa66ff95834e47b6f5c",
        "0e28bd0603b31c26250345a118408ffc",
        1,
        CRYPT_PADDING_NONE,
        0,
        "257f3fc84537158b68c8af111b1e9eb41f8841686ab1e94c6fd13a7f9f24d535309c340a1dd3d4966e439a41b9b97058e9072f613ef9c1ac958b872bea59f8831b578b63eec2d7155657f953f2c2375b",
        "ba4ebcdc894e6de54f8f1d7ccbb19e13d2ae0ca66c05c10e2f90bad2e9b8db94ee7770c3557927029d49fd2b3f80a01025af0e7a343237fb625dbdee85367ddfbd7f6664b511cdc7e832b2c4d91f1c0e",
    },
    {
        CRYPT_CIPHER_AES256_OFB,
        "75be95a6a54400b2e1b485e24ead18ed20a6261fba82d397166486b268c4a7c7",
        "3b395ad3450c56c8f1accab2a47be11e",
        0,
        CRYPT_PADDING_NONE,
        0,
        "52647fa67c0675753f725a8fd446622a2dff2841e61a46dc79792491b1090d4b61a2884a552ee56b890e16aa99b9c0e579a692e3666dd6941c29459a4a202009",
        "6892fc5c3d48f4e68a2c318f8157ee986fcf6c85a2ece70c0fdc709c504f0ab0b91606c4f13bc9bce5f1a1e0ce46960be86c35bc5982418097ba282d887608ea",
    },
};

static void SDV_KAE_AES_ENCRYPT_FUNC_TC006(void **state)
{
    CRYPT_AES_Hex *hex = (CRYPT_AES_Hex *)*state;

    Hex *key = NewHex();
    Hex *iv = NewHex();
    Hex *in = NewHex();
    Hex *out = NewHex();

    assert_int_equal(ConvertHex(hex->keyHex, key), 0);
    assert_int_equal(ConvertHex(hex->ivHex, iv), 0);
    assert_int_equal(ConvertHex(hex->inHex, in), 0);
    assert_int_equal(ConvertHex(hex->outHex, out), 0);

    uint32_t len = in->len;
    uint32_t totalLen = 0;

    CRYPT_EAL_LibCtx *libctx = CreateLoadLibCtx();
    CRYPT_EAL_CipherCtx *ctx = CRYPT_EAL_ProviderCipherNewCtx(libctx, hex->id, "provider=kaev1");
    assert_non_null(ctx);

    assert_int_equal(CRYPT_EAL_CipherInit(ctx, key->x, key->len, iv->x, iv->len, hex->temp), CRYPT_SUCCESS);
    assert_int_equal(CRYPT_EAL_CipherUpdate(ctx, in->x, in->len, in->x, &len), CRYPT_SUCCESS);
    totalLen = totalLen + len;
    len = in->len - totalLen;
    assert_int_equal(CRYPT_EAL_CipherFinal(ctx, in->x + totalLen, &len), CRYPT_SUCCESS);
    assert_memory_equal(in->x, out->x, out->len);

    FreeHex(key);
    FreeHex(iv);
    FreeHex(in);
    FreeHex(out);

    CRYPT_EAL_CipherFreeCtx(ctx);
    CRYPT_EAL_LibCtxFree(libctx);
}

static CRYPT_AES_Hex SDV_KAE_AES_ENCRYPT_FUNC_TC007_VEC[] = {
    {
        CRYPT_CIPHER_AES128_CBC,
        "3348aa51e9a45c2dbe33ccc47f96e8de",
        "19153c673160df2b1d38c28060e59b96",
        0,
        CRYPT_PADDING_ISO7816,
        1,
        "9b7cee827a26575afdbb7c7a329f887238052e3601a7917456ba61251c214763d5",
        "d5aed6c9622ec451a15db12819952b6752501cf05cdbf8cda34a457726ded978cb9f327a0aa53fd532eb1be6d2ae8fd3",
    },
    {
        CRYPT_CIPHER_AES128_CBC,
        "3348aa51e9a45c2dbe33ccc47f96e8de",
        "19153c673160df2b1d38c28060e59b96",
        0,
        CRYPT_PADDING_X923,
        1,
        "9b7cee827a26575afdbb7c7a329f887238052e3601a7917456ba61251c214763d5",
        "d5aed6c9622ec451a15db12819952b6752501cf05cdbf8cda34a457726ded9786d1816f7c5649d5937876ba0ef567a5d",
    },
    {
        CRYPT_CIPHER_AES128_CBC,
        "3348aa51e9a45c2dbe33ccc47f96e8de",
        "19153c673160df2b1d38c28060e59b96",
        0,
        CRYPT_PADDING_PKCS7,
        1,
        "9b7cee827a26575afdbb7c7a329f887238052e3601a7917456ba61251c214763d5",
        "d5aed6c9622ec451a15db12819952b6752501cf05cdbf8cda34a457726ded9780568af8935f1f764ed78e90f71189e4b",
    },
    {
        CRYPT_CIPHER_AES192_CBC,
        "16c93bb398f1fc0cf6d68fc7a5673cdf431fa147852b4a2d",
        "eaaeca2e07ddedf562f94df63f0a650f",
        0,
        CRYPT_PADDING_ISO7816,
        1,
        "c5ce958613bf741718c17444484ebaf1050ddcacb59b959017",
        "ed6a50e0c6921d52d6647f75d67b4fd5bc347477b3e501d4dadcdee7980a29c2",
    },
    {
        CRYPT_CIPHER_AES192_CBC,
        "16c93bb398f1fc0cf6d68fc7a5673cdf431fa147852b4a2d",
        "eaaeca2e07ddedf562f94df63f0a650f",
        0,
        CRYPT_PADDING_X923,
        1,
        "c5ce958613bf741718c17444484ebaf1050ddcacb59b959017",
        "ed6a50e0c6921d52d6647f75d67b4fd59371dfd343de75b79660c1f75c188ce9",
    },
    {
        CRYPT_CIPHER_AES192_CBC,
        "16c93bb398f1fc0cf6d68fc7a5673cdf431fa147852b4a2d",
        "eaaeca2e07ddedf562f94df63f0a650f",
        0,
        CRYPT_PADDING_PKCS7,
        1,
        "c5ce958613bf741718c17444484ebaf1050ddcacb59b959017",
        "ed6a50e0c6921d52d6647f75d67b4fd5cf4d9d94bcbd8b5915af4751d816d5aa",
    },
    {
        CRYPT_CIPHER_AES256_CBC,
        "0493ff637108af6a5b8e90ac1fdf035a3d4bafd1afb573be7ade9e8682e663e5",
        "c0cd2bebccbb6c49920bd5482ac756e8",
        0,
        CRYPT_PADDING_ISO7816,
        1,
        "8b37f9148df4bb25956be6310c73c8dc58ea9714ff49b643107b34c9bff096a94fedd6823526abc27a8e0b16616eee254ab4",
        "05d5c77729421b08b737e41119fa4438d1f570cc772a4d6c3df7ffeda0384ef84288ce37fc4c4c7d1125a499b051364cb1cd108e438552872a0ac79df78f4e3b",
    },
    {
        CRYPT_CIPHER_AES256_CBC,
        "0493ff637108af6a5b8e90ac1fdf035a3d4bafd1afb573be7ade9e8682e663e5",
        "c0cd2bebccbb6c49920bd5482ac756e8",
        0,
        CRYPT_PADDING_X923,
        1,
        "8b37f9148df4bb25956be6310c73c8dc58ea9714ff49b643107b34c9bff096a94fedd6823526abc27a8e0b16616eee254ab4",
        "05d5c77729421b08b737e41119fa4438d1f570cc772a4d6c3df7ffeda0384ef84288ce37fc4c4c7d1125a499b051364ce30c66a3ab285c6b09641f3afc57d81f",
    },
    {
        CRYPT_CIPHER_AES256_CBC,
        "0493ff637108af6a5b8e90ac1fdf035a3d4bafd1afb573be7ade9e8682e663e5",
        "c0cd2bebccbb6c49920bd5482ac756e8",
        0,
        CRYPT_PADDING_PKCS7,
        1,
        "8b37f9148df4bb25956be6310c73c8dc58ea9714ff49b643107b34c9bff096a94fedd6823526abc27a8e0b16616eee254ab4",
        "05d5c77729421b08b737e41119fa4438d1f570cc772a4d6c3df7ffeda0384ef84288ce37fc4c4c7d1125a499b051364c9696766e185b4fe717da75c7b1f88312",
    },
    {
        CRYPT_CIPHER_AES128_ECB,
        "edfdb257cb37cdf182c5455b0c0efebb",
        "",
        0,
        CRYPT_PADDING_PKCS7,
        1,
        "1695fe475421cace3557daca01f445ff",
        "7888beae6e7a426332a7eaa2f808e637916277ceb2c0c3c759e3dc4f0f33591f",
    },
    {
        CRYPT_CIPHER_AES192_ECB,
        "61396c530cc1749a5bab6fbcf906fe672d0c4ab201af4554",
        "",
        0,
        CRYPT_PADDING_PKCS7,
        1,
        "c5ce958613bf741718c17444484ebaf1050ddcacb59b959017",
        "beb17d7811ac92d00766d4fd21c2d5114e35e1834ecc73a54305e474c7abc242",
    },
    {
        CRYPT_CIPHER_AES256_ECB,
        "cc22da787f375711c76302bef0979d8eddf842829c2b99ef3dd04e23e54cc24b",
        "",
        0,
        CRYPT_PADDING_PKCS7,
        1,
        "8b37f9148df4bb25956be6310c73c8dc58ea9714ff49b643107b34c9bff096a94fedd6823526abc27a8e0b16616eee254ab4",
        "ce7ef65514eef925182082cdb5dc275e5da20070dcc54b26ef130a7a81b3e74de709e57fd3fda0631605104b61e5f57ea328d0b66f95a5eecff65bb881764b9c",
    },
};

static void SDV_KAE_AES_ENCRYPT_FUNC_TC007(void **state)
{
    CRYPT_AES_Hex *hex = (CRYPT_AES_Hex *)*state;

    Hex *key = NewHex();
    Hex *iv = NewHex();
    Hex *in = NewHex();
    Hex *out = NewHex();

    assert_int_equal(ConvertHex(hex->keyHex, key), 0);
    assert_int_equal(ConvertHex(hex->ivHex, iv), 0);
    assert_int_equal(ConvertHex(hex->inHex, in), 0);
    assert_int_equal(ConvertHex(hex->outHex, out), 0);

    uint8_t outTmp[MAX_OUTPUT] = {0};
    uint32_t totalLen = 0;
    uint32_t leftLen = MAX_OUTPUT;
    uint32_t len = MAX_OUTPUT;

    CRYPT_EAL_LibCtx *libctx = CreateLoadLibCtx();
    CRYPT_EAL_CipherCtx *ctxEnc = CRYPT_EAL_ProviderCipherNewCtx(libctx, hex->id, "provider=kaev1");
    assert_non_null(ctxEnc);

    assert_int_equal(CRYPT_EAL_CipherInit(ctxEnc, key->x, key->len, iv->x, iv->len, true), CRYPT_SUCCESS);
    assert_int_equal(CRYPT_EAL_CipherSetPadding(ctxEnc, hex->padding), CRYPT_SUCCESS);
    assert_int_equal(CRYPT_EAL_CipherUpdate(ctxEnc, in->x, in->len, outTmp, &len), CRYPT_SUCCESS);
    totalLen += len;
    leftLen = leftLen - len;
    assert_int_equal(CRYPT_EAL_CipherFinal(ctxEnc, outTmp + len, &leftLen), CRYPT_SUCCESS);
    totalLen += leftLen;

    assert_int_equal(totalLen, out->len);
    assert_memory_equal(out->x, outTmp, out->len);

    FreeHex(key);
    FreeHex(iv);
    FreeHex(in);
    FreeHex(out);

    CRYPT_EAL_CipherFreeCtx(ctxEnc);
    CRYPT_EAL_LibCtxFree(libctx);
}

void SDV_KAE_AES_ENCRYPT_FUNC_TC008(void **state)
{
    CRYPT_AES_Hex *hex = (CRYPT_AES_Hex *)*state;

    Hex *key = NewHex();
    Hex *iv = NewHex();
    Hex *in = NewHex();
    Hex *out = NewHex();

    assert_int_equal(ConvertHex(hex->keyHex, key), 0);
    assert_int_equal(ConvertHex(hex->ivHex, iv), 0);
    assert_int_equal(ConvertHex(hex->inHex, out), 0);
    assert_int_equal(ConvertHex(hex->outHex, in), 0);

    uint8_t result[MAX_OUTPUT] = {0};
    uint32_t totalLen = 0;
    uint32_t leftLen = MAX_OUTPUT;
    uint32_t len = MAX_OUTPUT;

    len = MAX_OUTPUT;
    leftLen = MAX_OUTPUT;

    CRYPT_EAL_LibCtx *libctx = CreateLoadLibCtx();
    CRYPT_EAL_CipherCtx *ctxDec = CRYPT_EAL_ProviderCipherNewCtx(libctx, hex->id, "provider=kaev1");
    assert_non_null(ctxDec);

    assert_int_equal(CRYPT_EAL_CipherInit(ctxDec, key->x, key->len, iv->x, iv->len, false), CRYPT_SUCCESS);
    assert_int_equal(CRYPT_EAL_CipherSetPadding(ctxDec, hex->padding), CRYPT_SUCCESS);
    assert_int_equal(CRYPT_EAL_CipherUpdate(ctxDec, in->x, in->len, result, &len), CRYPT_SUCCESS);
    totalLen += len;
    leftLen = leftLen - len;
    assert_int_equal(CRYPT_EAL_CipherFinal(ctxDec, result + len, &leftLen), CRYPT_SUCCESS);

    totalLen += leftLen;

    assert_int_equal(totalLen, out->len);
    assert_memory_equal(out->x, result, out->len);

    FreeHex(key);
    FreeHex(iv);
    FreeHex(in);
    FreeHex(out);

    CRYPT_EAL_CipherFreeCtx(ctxDec);
    CRYPT_EAL_LibCtxFree(libctx);
}

int main(void)
{
    struct CMUnitTest tests[GROUP_TEST_MAX] = {NULL};
    int testCnt = 0;
    tests[testCnt++] = (struct CMUnitTest)cmocka_unit_test(SDV_KAE_AES_PROVIDER_LOAD);
    for (size_t i = 0; i < sizeof(KAE_AES_ALG_ID) / sizeof(KAE_AES_ALG_ID[0]); i++) {
        tests[testCnt++] = (struct CMUnitTest)cmocka_unit_test_prestate(SDV_KAE_AES_NEW_API_TC001, &KAE_AES_ALG_ID[i]);
    }
    for (size_t i = 0; i < sizeof(SDV_KAE_AES_INIT_API_TC001_VEC) / sizeof(SDV_KAE_AES_INIT_API_TC001_VEC[0]); i++) {
        tests[testCnt++] = (struct CMUnitTest)cmocka_unit_test_prestate(SDV_KAE_AES_INIT_API_TC001,
                                                                        &SDV_KAE_AES_INIT_API_TC001_VEC[i]);
    }
    tests[testCnt++] = (struct CMUnitTest)cmocka_unit_test(SDV_KAE_AES_DEINIT_API_TC001);
    for (size_t i = 0; i < sizeof(SDV_KAE_AES_REINIT_API_TC001_VEC) / sizeof(SDV_KAE_AES_INIT_API_TC001_VEC[0]); i++) {
        tests[testCnt++] = (struct CMUnitTest)cmocka_unit_test_prestate(SDV_KAE_AES_REINIT_API_TC001,
                                                                        &SDV_KAE_AES_REINIT_API_TC001_VEC[i]);
    }
    for (size_t i = 0; i < sizeof(SDV_KAE_AES_UPDATE_API_TC001_VEC) / sizeof(SDV_KAE_AES_UPDATE_API_TC001_VEC[0]);
         i++) {
        tests[testCnt++] = (struct CMUnitTest)cmocka_unit_test_prestate(SDV_KAE_AES_UPDATE_API_TC001,
                                                                        &SDV_KAE_AES_UPDATE_API_TC001_VEC[i]);
    }
    for (size_t i = 0; i < sizeof(SDV_KAE_AES_FINAL_API_TC001_VEC) / sizeof(SDV_KAE_AES_FINAL_API_TC001_VEC[0]); i++) {
        tests[testCnt++] = (struct CMUnitTest)cmocka_unit_test_prestate(SDV_KAE_AES_FINAL_API_TC001,
                                                                        &SDV_KAE_AES_FINAL_API_TC001_VEC[i]);
    }
    for (size_t i = 0; i < sizeof(SDV_KAE_AES_CTRL_API_TC001_VEC) / sizeof(SDV_KAE_AES_CTRL_API_TC001_VEC[0]); i++) {
        tests[testCnt++] = (struct CMUnitTest)cmocka_unit_test_prestate(SDV_KAE_AES_CTRL_API_TC001,
                                                                        &SDV_KAE_AES_CTRL_API_TC001_VEC[i]);
    }
    for (size_t i = 0;
         i < sizeof(SDV_KAE_AES_SET_PADDING_API_TC001_VEC) / sizeof(SDV_KAE_AES_SET_PADDING_API_TC001_VEC[0]); i++) {
        tests[testCnt++] = (struct CMUnitTest)cmocka_unit_test_prestate(SDV_KAE_AES_SET_PADDING_API_TC001,
                                                                        &SDV_KAE_AES_SET_PADDING_API_TC001_VEC[i]);
    }
    for (size_t i = 0;
         i < sizeof(SDV_KAE_AES_GET_PADDING_API_TC001_VEC) / sizeof(SDV_KAE_AES_GET_PADDING_API_TC001_VEC[0]); i++) {
        tests[testCnt++] = (struct CMUnitTest)cmocka_unit_test_prestate(SDV_KAE_AES_GET_PADDING_API_TC001,
                                                                        &SDV_KAE_AES_GET_PADDING_API_TC001_VEC[i]);
    }
    for (size_t i = 0; i < sizeof(SDV_KAE_AES_ENCRYPT_FUNC_TC001_VEC) / sizeof(SDV_KAE_AES_ENCRYPT_FUNC_TC001_VEC[0]);
         i++) {
        tests[testCnt++] = (struct CMUnitTest)cmocka_unit_test_prestate(SDV_KAE_AES_ENCRYPT_FUNC_TC001,
                                                                        &SDV_KAE_AES_ENCRYPT_FUNC_TC001_VEC[i]);
    }
    for (size_t i = 0; i < sizeof(SDV_KAE_AES_ENCRYPT_FUNC_TC002_VEC) / sizeof(SDV_KAE_AES_ENCRYPT_FUNC_TC002_VEC[0]);
         i++) {
        tests[testCnt++] = (struct CMUnitTest)cmocka_unit_test_prestate(SDV_KAE_AES_ENCRYPT_FUNC_TC002,
                                                                        &SDV_KAE_AES_ENCRYPT_FUNC_TC002_VEC[i]);
    }
    for (size_t i = 0; i < sizeof(SDV_KAE_AES_ENCRYPT_FUNC_TC003_VEC) / sizeof(SDV_KAE_AES_ENCRYPT_FUNC_TC003_VEC[0]);
         i++) {
        tests[testCnt++] = (struct CMUnitTest)cmocka_unit_test_prestate(SDV_KAE_AES_ENCRYPT_FUNC_TC003,
                                                                        &SDV_KAE_AES_ENCRYPT_FUNC_TC003_VEC[i]);
    }
    for (size_t i = 0; i < sizeof(SDV_KAE_AES_ENCRYPT_FUNC_TC004_VEC) / sizeof(SDV_KAE_AES_ENCRYPT_FUNC_TC004_VEC[0]);
         i++) {
        tests[testCnt++] = (struct CMUnitTest)cmocka_unit_test_prestate(SDV_KAE_AES_ENCRYPT_FUNC_TC004,
                                                                        &SDV_KAE_AES_ENCRYPT_FUNC_TC004_VEC[i]);
    }
    for (size_t i = 0; i < sizeof(SDV_KAE_AES_ENCRYPT_FUNC_TC005_VEC) / sizeof(SDV_KAE_AES_ENCRYPT_FUNC_TC005_VEC[0]);
         i++) {
        tests[testCnt++] = (struct CMUnitTest)cmocka_unit_test_prestate(SDV_KAE_AES_ENCRYPT_FUNC_TC005,
                                                                        &SDV_KAE_AES_ENCRYPT_FUNC_TC005_VEC[i]);
    }
    for (size_t i = 0; i < sizeof(SDV_KAE_AES_ENCRYPT_FUNC_TC006_VEC) / sizeof(SDV_KAE_AES_ENCRYPT_FUNC_TC006_VEC[0]);
         i++) {
        tests[testCnt++] = (struct CMUnitTest)cmocka_unit_test_prestate(SDV_KAE_AES_ENCRYPT_FUNC_TC006,
                                                                        &SDV_KAE_AES_ENCRYPT_FUNC_TC006_VEC[i]);
    }
    for (size_t i = 0; i < sizeof(SDV_KAE_AES_ENCRYPT_FUNC_TC007_VEC) / sizeof(SDV_KAE_AES_ENCRYPT_FUNC_TC007_VEC[0]);
         i++) {
        tests[testCnt++] = (struct CMUnitTest)cmocka_unit_test_prestate(SDV_KAE_AES_ENCRYPT_FUNC_TC007,
                                                                        &SDV_KAE_AES_ENCRYPT_FUNC_TC007_VEC[i]);
    }
    for (size_t i = 0; i < sizeof(SDV_KAE_AES_ENCRYPT_FUNC_TC007_VEC) / sizeof(SDV_KAE_AES_ENCRYPT_FUNC_TC007_VEC[0]);
         i++) {
        tests[testCnt++] = (struct CMUnitTest)cmocka_unit_test_prestate(SDV_KAE_AES_ENCRYPT_FUNC_TC008,
                                                                        &SDV_KAE_AES_ENCRYPT_FUNC_TC007_VEC[i]);
    }
    return cmocka_run_group_tests(tests, NULL, NULL);
}